TTLabs / EvaporateJS

Javascript library for browser to S3 multipart resumable uploads
1.82k stars 207 forks source link

Uploading to an external bucket with pre-calculated headers #428

Open fawcilize opened 5 years ago

fawcilize commented 5 years ago

I'm using Vzaar for video hosting/processing. For multi-part uploads, I first go to their endpoint and they provide me with the headers:


{
  "data": {
    "x-amz-credential": "REDACTED/20190410/us-east-1/s3/aws4_request",
    "x-amz-algorithm": "AWS4-HMAC-SHA256",
    "x-amz-date": "20190410T133152Z",
    "x-amz-signature": "REDACTED",
    "key": "vzaar/REDACTED/${filename}",
    "acl": "private",
    "policy": "REDACTEDbase64==",
    "success_action_status": "201",
    "guid": "REDACTED",
    "bucket": "REDACTED",
    "upload_hostname": "https://REDACTED.s3.amazonaws.com",
    "part_size": "9MB",
    "part_size_in_bytes": 9437184,
    "parts": 1
  }
}

However, I'm not clear how to use evaporate with these pre-calculated headers. I'd like to avoid recalculating everything again. Is there a way to pass the headers in directly? I saw xAmzHeadersAtInitiate but I'm not sure how to access that or if it works.

Here is my code, tweaked from one of the examples:

var customAuthMethod = function customAuthMethod() {
    return Promise.resolve(vzaarUploadParams['X-Amz-Signature']);
}

Evaporate.create({
    customAuthMethod,
    aws_key: vzaarUploadParams.aws_key,
    bucket: vzaarUploadParams.bucket,
    computeContentMd5: true,
    cryptoMd5Method: function (data) { return AWS.util.crypto.md5(data, 'base64'); },
    cryptoHexEncodedHash256: function (data) { return AWS.util.crypto.sha256(data, 'hex'); }
}).then(
    function onEvaporateCreated(_e_) {
    for (var i = 0; i < files.length; i++) {
        var promise = _e_.add({
            name: files[i].name,
            file: files[i],
            progress: function (progress) {
                console.log('making progress: ' + progress);
            }
        }, {
            xAmzHeadersAtInitiate: vzaarUploadParams,
        }).then(function (awsKey) {
            console.log(awsKey, 'complete!');
        });
    }
    },
    function onCreateEvaporateFailed(reason) {
    console.log('Evaporate failed to initialize: ', reason)
    }
);
jakubzitny commented 5 years ago

Yes, the xAmzHeadersAtInitiate, xAmzHeadersAtUpload and xAmzHeadersAtComplete work. You pass the headers via these. What else do you need? Where is the problem? What do you want to access? You pass them in [add](https://github.com/TTLabs/EvaporateJS/wiki/Evaporate.prototype.add())