question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pre-PR discussion: upload progress for Storage class

See original GitHub issue

Hi 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)

@mlabieniec @mbahar

    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:

  1. If we’re using TypeScript, we should use it to its fullest extent. Why aren’t we using an interface, for example, for the options parameter here?
  2. 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:closed
  • Created 5 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

7reactions
ffxsamcommented, Oct 3, 2018

PR opened, awaiting review.

2reactions
haverchuckcommented, Sep 27, 2018

This looks great to me!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found