开发者

plupload / flash / Amazon S3 / unique_names

I am working on an implementation of plupload using flash to upload files to Amazon S3. I have the uploading of the files to S3 working perfectly except for one small problem. The i开发者_JAVA百科ssue is the setting 'unique_names' is not working. I always end up with file in S3 with the original file name. After some debugging I do see that the unique file name is created and passed on to the flash SWF for uploading but it is never used. Any help would be greatly appreciated.


This might help: http://www.plupload.com/punbb/viewtopic.php?pid=4321#p4321

You have to set up a file renaming function by writing custom code for the UploadFile Event.

This is what I did:

        preinit : {

         UploadFile: function(up, file) {
             up.settings.multipart_params.key = 'adjsdlasjdasdas.jpg';
         }
        },

Replace 'adjsdlasjdasdas.jpg'; with your custom function for renaming.


I found the following solution at https://github.com/moxiecode/plupload/issues/254. This is how I used the code. First it removes the extension, then I create a unique ID and last I add the extension. I also added a folder name using a php variable, this is not necessary but I think it will keep your bucket more organized.

preinit : {

    BeforeUpload : function(up,file){

        // Get the file extension 
        var file_name = file.name;
        var extension = file_name.split(".").pop();

        // Create a random ID
        var new_id = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        var d = new Date();
        var n = d.getTime();
        for( var i=0; i < 10; i++ )
        new_id += possible.charAt(Math.floor(Math.random() * possible.length));

        // Change the name NOTE I use a php variable to create a folder on S3
        up.settings.multipart_params.key = "<?php echo $foldername ?>/"+n+new_id+"."+extension;
        up.settings.multipart_params.Filename = "<?php echo $foldername ?>/"+n+new_id+"."+extension;
        }

},


you can check if the file is unique before you upload the file. Here is my code.

UploadFile: function(up, file) {
        pluploadlog('[UploadFile]', file);
        // You can override settings before the file is uploaded
        var filename = file.name;
        file.newname = '01'+ filename ;///add your own function here to create something unique. 
         up.settings.multipart_params = {
                   'key': mybucketsubdirectory + filename, 
                  'Filename': filename, 
                  'acl': 'public-read',
                  'Content-Type': 'image/jpeg',
                  'AWSAccessKeyId' : '<?php echo $accessKeyId; ?>',
                  'policy': '<?php echo $policy; ?>',
                  'signature': '<?php echo $signature; ?>'
                };
      },

Then on your file upload look at the file.newname you added previously.

FileUploaded: function(up, file, info) {
          if (file.status == 5){
             console.log(file.newname); // If you need to get back the new unique name
            uploadSuccess(file, info); 
          }else{
            showItemError(file);
          }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜