Sunday, April 3, 2016

Rails upload to AWS creating .zip.cpgz file loop

Leave a Comment

I'm running into a weird situation where some files, specifically ZIP format, when uploaded to AWS within my Rails app are corrupted/converted. When downloaded and decompressed they turn into a CPGZ format, which decompresses back into a ZIP, and infinitely does this.

I haven't noticed a pattern that causes this, so it's seemingly sporadic, and can confirm that the files are not corrupt before upload. The only other issue/topic I've found on this relates to PHP, and seems to be different circumstances.

I am using AWS SDK for Ruby v1 (not v2 because of my Rails version) and jQuery-File-Upload. Since some of the files are large, I am using chunked uploads.

In my controller, the presigned POST URL is created like this:

S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}-${filename}", success_action_status: '201') 

And jQuery File Upload is set up like so (some parts removed for brevity):

this.$el.fileupload({   fileInput: this.uploadField, // this is an <input type="file">   url: this.awsURL, // https://BUCKET.s3.amazonaws.com/   formData: JSON.parse(this.awsData), // {"AWSAccessKeyId":"...","key":"uploads/1234-${filename}","policy":"...","signature":"...","success_action_status":"201"}   type: 'POST',   autoUpload: true,   paramName: 'file',   dataType: 'XML',   replaceFileInput: false,   maxChunkSize: 1000000,   add: function(event, data) {     var file = data.files[0];     var fileType = file.type;      // Check file type     if (~'ai sketch psd jpg jpeg png zip ttf woff eot gif'.indexOf(fileType.toLowerCase())) {       return alert('Sorry, that file type is not supported');     };      data.submit();   },   progress: function(event, data) {     // Display progress   },   done: function(event, data) {     var file = data.files[0];     var fileName = file.name.replace(/ /g,"_");     var item = _this.uploadedItems[fileName];     var key = $(data.jqXHR.responseXML).find("Key").text();     // awsHost = BUCKET.s3.amazonaws.com     var url = '//' + _this.awsHost + '/' + key;      // Set form values using above info   },   fail: function(event, data) {     // Alert failure   } }); 

Has anyone experienced this? It's very frustrating.

1 Answers

Answers 1

Set content-type to application/zip when you sending the request.

SEE https://github.com/aws/aws-sdk-ruby/blob/aws-sdk-v1/lib/aws/s3/presigned_post.rb

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment