Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@aws-amplify/storage

Package Overview
Dependencies
Maintainers
7
Versions
2060
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aws-amplify/storage - npm Package Compare versions

Comparing version 1.0.20-unstable.0 to 1.0.20-unstable.1

lib/Providers/AWSS3Provider.d.ts

8

CHANGELOG.md

@@ -6,2 +6,10 @@ # Change Log

<a name="1.0.20-unstable.1"></a>
## [1.0.20-unstable.1](https://github.com/aws/aws-amplify/compare/@aws-amplify/storage@1.0.20-unstable.0...@aws-amplify/storage@1.0.20-unstable.1) (2018-11-19)
**Note:** Version bump only for package @aws-amplify/storage
<a name="1.0.20-unstable.0"></a>

@@ -8,0 +16,0 @@ ## [1.0.20-unstable.0](https://github.com/aws/aws-amplify/compare/@aws-amplify/storage@1.0.19...@aws-amplify/storage@1.0.20-unstable.0) (2018-11-15)

3

lib/index.d.ts
import StorageClass from './Storage';
import { StorageProvider } from './types';
declare const Storage: StorageClass;
export default Storage;
export { StorageClass };
export { StorageProvider };
export * from './Providers';

9

lib/index.js

@@ -14,2 +14,5 @@ "use strict";

*/
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });

@@ -23,4 +26,5 @@ var Storage_1 = require("./Storage");

logger.debug('Create Storage Instance');
_instance = new Storage_1.default(null);
_instance.vault = new Storage_1.default({ level: 'private' });
_instance = new Storage_1.default();
_instance.vault = new Storage_1.default();
_instance.vault.configure({ level: 'private' });
var old_configure_1 = _instance.configure;

@@ -37,2 +41,3 @@ _instance.configure = function (options) {

exports.default = Storage;
__export(require("./Providers"));
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

import { StorageOptions } from './types';
import { StorageProvider } from './types';
/**

@@ -9,3 +9,4 @@ * Provide storage methods to use AWS S3

*/
private _options;
private _config;
private _pluggables;
/**

@@ -16,13 +17,28 @@ * @public

/**
* Initialize Storage with AWS configurations
* @param {Object} options - Configuration object for storage
* Initialize Storage
* @param {Object} config - Configuration object for storage
*/
constructor(options: StorageOptions);
constructor();
getModuleName(): string;
/**
* Configure Storage part with aws configuration
* @param {Object} config - Configuration of the Storage
* add plugin into Storage category
* @param {Object} pluggable - an instance of the plugin
*/
addPluggable(pluggable: StorageProvider): {};
/**
* Get the plugin object
* @param providerName - the name of the plugin
*/
getPluggable(providerName: string): StorageProvider;
/**
* Remove the plugin object
* @param providerName - the name of the plugin
*/
removePluggable(providerName: string): void;
/**
* Configure Storage
* @param {Object} config - Configuration object for storage
* @return {Object} - Current configuration
*/
configure(options?: any): any;
configure(config?: any): any;
/**

@@ -32,41 +48,29 @@ * Get a presigned URL of the file or the object data when download:true

* @param {String} key - key of the object
* @param {Object} [options] - { level : private|protected|public, download: true|false }
* @return - A promise resolves to Amazon S3 presigned URL on success
* @param {Object} [config] - { level : private|protected|public, download: true|false }
* @return - A promise resolves to either a presigned url or the object
*/
get(key: string, options?: any): Promise<String | Object>;
get(key: string, config?: any): Promise<String | Object>;
/**
* Put a file in S3 bucket specified to configure method
* Put a file in storage bucket specified to configure method
* @param {Stirng} key - key of the object
* @param {Object} object - File to be put in Amazon S3 bucket
* @param {Object} [options] - { level : private|protected|public, contentType: MIME Types,
* @param {Object} object - File to be put in bucket
* @param {Object} [config] - { level : private|protected|public, contentType: MIME Types,
* progressCallback: function }
* @return - promise resolves to object on success
*/
put(key: string, object: any, options?: any): Promise<Object>;
put(key: string, object: any, config?: any): Promise<Object>;
/**
* Remove the object for specified key
* @param {String} key - key of the object
* @param {Object} [options] - { level : private|protected|public }
* @param {Object} [config] - { level : private|protected|public }
* @return - Promise resolves upon successful removal of the object
*/
remove(key: string, options?: any): Promise<any>;
remove(key: string, config?: any): Promise<any>;
/**
* List bucket objects relative to the level and prefix specified
* @param {String} path - the path that contains objects
* @param {Object} [options] - { level : private|protected|public }
* @param {Object} [config] - { level : private|protected|public }
* @return - Promise resolves to list of keys for all objects in path
*/
list(path: any, options?: any): Promise<any>;
/**
* @private
*/
_ensureCredentials(): any;
/**
* @private
*/
private _prefix;
/**
* @private
*/
private _createS3;
list(path: any, config?: any): Promise<any>;
}

@@ -51,9 +51,5 @@ "use strict";

var core_1 = require("@aws-amplify/core");
var S3 = require("aws-sdk/clients/s3");
var AWSS3Provider_1 = require("./Providers/AWSS3Provider");
var logger = new core_1.ConsoleLogger('StorageClass');
var dispatchStorageEvent = function (track, attrs, metrics) {
if (track) {
core_1.Hub.dispatch('storage', { attrs: attrs, metrics: metrics }, 'Storage');
}
};
var DEFAULT_PROVIDER = 'AWSS3';
/**

@@ -64,8 +60,13 @@ * Provide storage methods to use AWS S3

/**
* Initialize Storage with AWS configurations
* @param {Object} options - Configuration object for storage
* Initialize Storage
* @param {Object} config - Configuration object for storage
*/
function StorageClass(options) {
this._options = options;
logger.debug('Storage Options', this._options);
function StorageClass() {
this._config = {};
this._pluggables = [];
logger.debug('Storage Options', this._config);
this.get = this.get.bind(this);
this.put = this.put.bind(this);
this.remove = this.remove.bind(this);
this.list = this.list.bind(this);
}

@@ -76,20 +77,68 @@ StorageClass.prototype.getModuleName = function () {

/**
* Configure Storage part with aws configuration
* @param {Object} config - Configuration of the Storage
* add plugin into Storage category
* @param {Object} pluggable - an instance of the plugin
*/
StorageClass.prototype.addPluggable = function (pluggable) {
if (pluggable && pluggable.getCategory() === 'Storage') {
this._pluggables.push(pluggable);
var config = {};
// for backward compatibility
if (pluggable.getProviderName() === DEFAULT_PROVIDER && !this._config[DEFAULT_PROVIDER]) {
config = pluggable.configure(this._config);
}
else {
config = pluggable.configure(this._config[pluggable.getProviderName()]);
}
return config;
}
};
/**
* Get the plugin object
* @param providerName - the name of the plugin
*/
StorageClass.prototype.getPluggable = function (providerName) {
var pluggable = this._pluggables.find(function (pluggable) { return pluggable.getProviderName() === providerName; });
if (pluggable === undefined) {
logger.debug('No plugin found with providerName', providerName);
return null;
}
else
return pluggable;
};
/**
* Remove the plugin object
* @param providerName - the name of the plugin
*/
StorageClass.prototype.removePluggable = function (providerName) {
this._pluggables = this._pluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
return;
};
/**
* Configure Storage
* @param {Object} config - Configuration object for storage
* @return {Object} - Current configuration
*/
StorageClass.prototype.configure = function (options) {
StorageClass.prototype.configure = function (config) {
var _this = this;
logger.debug('configure Storage');
var opt = options ? options.Storage || options : {};
if (options['aws_user_files_s3_bucket']) {
opt = {
bucket: options['aws_user_files_s3_bucket'],
region: options['aws_user_files_s3_bucket_region']
};
}
this._options = Object.assign({}, this._options, opt);
if (!this._options.bucket) {
if (!config)
return this._config;
var amplifyConfig = core_1.Parser.parseMobilehubConfig(config);
this._config = Object.assign({}, this._config, amplifyConfig.Storage);
if (!this._config.bucket) {
logger.debug('Do not have bucket yet');
}
return this._options;
this._pluggables.forEach(function (pluggable) {
// for backward compatibility
if (pluggable.getProviderName() === DEFAULT_PROVIDER && !_this._config[DEFAULT_PROVIDER]) {
pluggable.configure(_this._config);
}
else {
pluggable.configure(_this._config[pluggable.getProviderName()]);
}
});
if (this._pluggables.length === 0) {
this.addPluggable(new AWSS3Provider_1.default());
}
return this._config;
};

@@ -100,56 +149,16 @@ /**

* @param {String} key - key of the object
* @param {Object} [options] - { level : private|protected|public, download: true|false }
* @return - A promise resolves to Amazon S3 presigned URL on success
* @param {Object} [config] - { level : private|protected|public, download: true|false }
* @return - A promise resolves to either a presigned url or the object
*/
StorageClass.prototype.get = function (key, options) {
StorageClass.prototype.get = function (key, config) {
return __awaiter(this, void 0, void 0, function () {
var credentialsOK, opt, bucket, region, credentials, level, download, track, expires, prefix, final_key, s3, params;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._ensureCredentials()];
case 1:
credentialsOK = _a.sent();
if (!credentialsOK) {
return [2 /*return*/, Promise.reject('No credentials')];
}
opt = Object.assign({}, this._options, options);
bucket = opt.bucket, region = opt.region, credentials = opt.credentials, level = opt.level, download = opt.download, track = opt.track, expires = opt.expires;
prefix = this._prefix(opt);
final_key = prefix + key;
s3 = this._createS3(opt);
logger.debug('get ' + key + ' from ' + final_key);
params = {
Bucket: bucket,
Key: final_key
};
if (download === true) {
return [2 /*return*/, new Promise(function (res, rej) {
s3.getObject(params, function (err, data) {
if (err) {
dispatchStorageEvent(track, { method: 'get', result: 'failed' }, null);
rej(err);
}
else {
dispatchStorageEvent(track, { method: 'get', result: 'success' }, { fileSize: Number(data.Body['length']) });
res(data);
}
});
})];
}
if (expires) {
params.Expires = expires;
}
return [2 /*return*/, new Promise(function (res, rej) {
try {
var url = s3.getSignedUrl('getObject', params);
dispatchStorageEvent(track, { method: 'get', result: 'success' }, null);
res(url);
}
catch (e) {
logger.warn('get signed url error', e);
dispatchStorageEvent(track, { method: 'get', result: 'failed' }, null);
rej(e);
}
})];
var _a, provider, prov;
return __generator(this, function (_b) {
_a = (config.provider || {}).provider, provider = _a === void 0 ? DEFAULT_PROVIDER : _a;
prov = this._pluggables.find(function (pluggable) { return pluggable.getProviderName() === provider; });
if (prov === undefined) {
logger.debug('No plugin found with providerName', provider);
Promise.reject('No plugin found in Storage for the provider');
}
return [2 /*return*/, prov.get(key, config)];
});

@@ -159,76 +168,20 @@ });

/**
* Put a file in S3 bucket specified to configure method
* Put a file in storage bucket specified to configure method
* @param {Stirng} key - key of the object
* @param {Object} object - File to be put in Amazon S3 bucket
* @param {Object} [options] - { level : private|protected|public, contentType: MIME Types,
* @param {Object} object - File to be put in bucket
* @param {Object} [config] - { level : private|protected|public, contentType: MIME Types,
* progressCallback: function }
* @return - promise resolves to object on success
*/
StorageClass.prototype.put = function (key, object, options) {
StorageClass.prototype.put = function (key, object, config) {
return __awaiter(this, void 0, void 0, function () {
var credentialsOK, opt, bucket, region, credentials, level, track, progressCallback, contentType, contentDisposition, cacheControl, expires, metadata, type, prefix, final_key, s3, params, upload, data, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._ensureCredentials()];
case 1:
credentialsOK = _a.sent();
if (!credentialsOK) {
return [2 /*return*/, Promise.reject('No credentials')];
}
opt = Object.assign({}, this._options, options);
bucket = opt.bucket, region = opt.region, credentials = opt.credentials, level = opt.level, track = opt.track, progressCallback = opt.progressCallback;
contentType = opt.contentType, contentDisposition = opt.contentDisposition, cacheControl = opt.cacheControl, expires = opt.expires, metadata = opt.metadata;
type = contentType ? contentType : 'binary/octet-stream';
prefix = this._prefix(opt);
final_key = prefix + key;
s3 = this._createS3(opt);
logger.debug('put ' + key + ' to ' + final_key);
params = {
Bucket: bucket,
Key: final_key,
Body: object,
ContentType: type
};
if (cacheControl) {
params.CacheControl = cacheControl;
}
if (contentDisposition) {
params.ContentDisposition = contentDisposition;
}
if (expires) {
params.Expires = expires;
}
if (metadata) {
params.Metadata = metadata;
}
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
upload = s3
.upload(params)
.on('httpUploadProgress', function (progress) {
if (progressCallback) {
if (typeof progressCallback === 'function') {
progressCallback(progress);
}
else {
logger.warn('progressCallback should be a function, not a ' + typeof progressCallback);
}
}
});
return [4 /*yield*/, upload.promise()];
case 3:
data = _a.sent();
logger.debug('upload result', data);
dispatchStorageEvent(track, { method: 'put', result: 'success' }, null);
return [2 /*return*/, {
key: data.Key.substr(prefix.length)
}];
case 4:
e_1 = _a.sent();
logger.warn("error uploading", e_1);
dispatchStorageEvent(track, { method: 'put', result: 'failed' }, null);
throw e_1;
case 5: return [2 /*return*/];
var _a, provider, prov;
return __generator(this, function (_b) {
_a = (config.provider || {}).provider, provider = _a === void 0 ? DEFAULT_PROVIDER : _a;
prov = this._pluggables.find(function (pluggable) { return pluggable.getProviderName() === provider; });
if (prov === undefined) {
logger.debug('No plugin found with providerName', provider);
Promise.reject('No plugin found in Storage for the provider');
}
return [2 /*return*/, prov.put(key, object, config)];
});

@@ -240,39 +193,16 @@ });

* @param {String} key - key of the object
* @param {Object} [options] - { level : private|protected|public }
* @param {Object} [config] - { level : private|protected|public }
* @return - Promise resolves upon successful removal of the object
*/
StorageClass.prototype.remove = function (key, options) {
StorageClass.prototype.remove = function (key, config) {
return __awaiter(this, void 0, void 0, function () {
var credentialsOK, opt, bucket, region, credentials, level, track, prefix, final_key, s3, params;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._ensureCredentials()];
case 1:
credentialsOK = _a.sent();
if (!credentialsOK) {
return [2 /*return*/, Promise.reject('No credentials')];
}
opt = Object.assign({}, this._options, options);
bucket = opt.bucket, region = opt.region, credentials = opt.credentials, level = opt.level, track = opt.track;
prefix = this._prefix(opt);
final_key = prefix + key;
s3 = this._createS3(opt);
logger.debug('remove ' + key + ' from ' + final_key);
params = {
Bucket: bucket,
Key: final_key
};
return [2 /*return*/, new Promise(function (res, rej) {
s3.deleteObject(params, function (err, data) {
if (err) {
dispatchStorageEvent(track, { method: 'remove', result: 'failed' }, null);
rej(err);
}
else {
dispatchStorageEvent(track, { method: 'remove', result: 'success' }, null);
res(data);
}
});
})];
var _a, provider, prov;
return __generator(this, function (_b) {
_a = (config.provider || {}).provider, provider = _a === void 0 ? DEFAULT_PROVIDER : _a;
prov = this._pluggables.find(function (pluggable) { return pluggable.getProviderName() === provider; });
if (prov === undefined) {
logger.debug('No plugin found with providerName', provider);
Promise.reject('No plugin found in Storage for the provider');
}
return [2 /*return*/, prov.remove(key, config)];
});

@@ -284,109 +214,19 @@ });

* @param {String} path - the path that contains objects
* @param {Object} [options] - { level : private|protected|public }
* @param {Object} [config] - { level : private|protected|public }
* @return - Promise resolves to list of keys for all objects in path
*/
StorageClass.prototype.list = function (path, options) {
StorageClass.prototype.list = function (path, config) {
return __awaiter(this, void 0, void 0, function () {
var credentialsOK, opt, bucket, region, credentials, level, download, track, prefix, final_path, s3, params;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._ensureCredentials()];
case 1:
credentialsOK = _a.sent();
if (!credentialsOK) {
return [2 /*return*/, Promise.reject('No credentials')];
}
opt = Object.assign({}, this._options, options);
bucket = opt.bucket, region = opt.region, credentials = opt.credentials, level = opt.level, download = opt.download, track = opt.track;
prefix = this._prefix(opt);
final_path = prefix + path;
s3 = this._createS3(opt);
logger.debug('list ' + path + ' from ' + final_path);
params = {
Bucket: bucket,
Prefix: final_path
};
return [2 /*return*/, new Promise(function (res, rej) {
s3.listObjects(params, function (err, data) {
if (err) {
logger.warn('list error', err);
dispatchStorageEvent(track, { method: 'list', result: 'failed' }, null);
rej(err);
}
else {
var list = data.Contents.map(function (item) {
return {
key: item.Key.substr(prefix.length),
eTag: item.ETag,
lastModified: item.LastModified,
size: item.Size
};
});
dispatchStorageEvent(track, { method: 'list', result: 'success' }, null);
logger.debug('list', list);
res(list);
}
});
})];
var _a, provider, prov;
return __generator(this, function (_b) {
_a = (config.provider || {}).provider, provider = _a === void 0 ? DEFAULT_PROVIDER : _a;
prov = this._pluggables.find(function (pluggable) { return pluggable.getProviderName() === provider; });
if (prov === undefined) {
logger.debug('No plugin found with providerName', provider);
Promise.reject('No plugin found in Storage for the provider');
}
return [2 /*return*/, prov.list(path, config)];
});
});
};
/**
* @private
*/
StorageClass.prototype._ensureCredentials = function () {
// commented
// will cause bug if another user logged in without refreshing page
// if (this._options.credentials) { return Promise.resolve(true); }
var _this = this;
return core_1.Credentials.get()
.then(function (credentials) {
if (!credentials)
return false;
var cred = core_1.Credentials.shear(credentials);
logger.debug('set credentials for storage', cred);
_this._options.credentials = cred;
return true;
})
.catch(function (err) {
logger.warn('ensure credentials error', err);
return false;
});
};
/**
* @private
*/
StorageClass.prototype._prefix = function (options) {
var credentials = options.credentials, level = options.level;
var customPrefix = options.customPrefix || {};
var identityId = options.identityId || credentials.identityId;
var privatePath = (customPrefix.private !== undefined ? customPrefix.private : 'private/') + identityId + '/';
var protectedPath = (customPrefix.protected !== undefined ?
customPrefix.protected : 'protected/') + identityId + '/';
var publicPath = customPrefix.public !== undefined ? customPrefix.public : 'public/';
switch (level) {
case 'private':
return privatePath;
case 'protected':
return protectedPath;
default:
return publicPath;
}
};
/**
* @private
*/
StorageClass.prototype._createS3 = function (options) {
var bucket = options.bucket, region = options.region, credentials = options.credentials;
core_1.AWS.config.update({
region: region,
credentials: credentials
});
return new S3({
apiVersion: '2006-03-01',
params: { Bucket: bucket },
region: region
});
};
return StorageClass;

@@ -393,0 +233,0 @@ }());

export * from './Storage';
export * from './Provider';

@@ -7,4 +7,4 @@ /**

region?: string;
level?: string;
credentials?: object;
level?: string;
}
"use strict";
/*
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=Storage.js.map
{
"name": "@aws-amplify/storage",
"version": "1.0.20-unstable.0",
"version": "1.0.20-unstable.1",
"description": "Storage category of aws-amplify",

@@ -53,3 +53,3 @@ "main": "./lib/index.js",

"dependencies": {
"@aws-amplify/core": "^1.0.19-unstable.0"
"@aws-amplify/core": "^1.0.19-unstable.1"
},

@@ -56,0 +56,0 @@ "jest": {

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc