Pre-PR discussion: upload progress for Storage class
See original GitHub issueHi all,
I’ve whipped up a modification to Storage.put
that I wanted to run by the team before opening a PR. What do you think of the below? (also, see below the code for some questions)
public async put(key: string, object, options?): Promise<Object> {
const credentialsOK = await this._ensureCredentials();
if (!credentialsOK) { return Promise.reject('No credentials'); }
const opt = Object.assign({}, this._options, options);
const { bucket, region, credentials, level, track, progressCallback } = opt;
const { contentType, contentDisposition, cacheControl, expires, metadata } = opt;
const type = contentType ? contentType : 'binary/octet-stream';
const prefix = this._prefix(opt);
const final_key = prefix + key;
const s3 = this._createS3(opt);
logger.debug('put ' + key + ' to ' + final_key);
const params: any = {
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; }
try {
const upload = s3
.upload(params)
.on('httpUploadProgress', ({ loaded }) => {
if (progressCallback) {
progressCallback(loaded);
}
});
const data = await upload.promise();
logger.debug('upload result', data);
dispatchStorageEvent(
track,
{ method: 'put', result: 'success' },
null);
return {
key: data.Key.substr(prefix.length)
};
} catch (e) {
logger.warn("error uploading", e);
dispatchStorageEvent(
track,
{ method: 'put', result: 'failed' },
null);
throw e;
}
}
Some questions that came to mind while I was working on this:
- If we’re using TypeScript, we should use it to its fullest extent. Why aren’t we using an
interface
, for example, for theoptions
parameter here? - If we’re using async/await, we might want to start moving towards using it universally (rather than
Promise
) and using try/catch blocks instead of an(err, res) => {}
callback, just for the sake of code legibility.
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (13 by maintainers)
Top Results From Across the Web
Storage Progress Bar · Discussion #6879 · supabase ... - GitHub
I would like to add a file upload progress bar to my app. Does anyone have example code or recommendations on how to...
Read more >Files - Canvas, CSU - Colorado State University
User files include profile pictures, uploaded assignment submissions, and other files uploaded to your personal Canvas file storage area.
Read more >Web API - Get progress when uploading to Azure storage
First is that i have to split the file manually into chunks, and upload them asynchronously using the PutBlockAsync method from Microsoft.WindowsAzure.Storage.
Read more >Basic: Use Amazon S3 Storage Classes - DEV Community
The default storage class. If you don't specify the storage class when you upload an object, Amazon S3 assigns the S3 Standard storage...
Read more >Google TPM interview (questions, process and prep)
This is a complete guide to Google technical program manager (TPM) interviews (also applies to GCP). Learn the interview process, practice with example ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
PR opened, awaiting review.
This looks great to me!