简体   繁体   English

上传后立即获取上传的 S3 文件的版本 ID?

[英]Get version ID of uploaded S3 file right after upload?

I'm dealing with the inevitable issue of users being able to overwrite files in my bucket via a JavaScript S3 upload.我正在处理不可避免的问题,即用户能够通过 JavaScript S3 上传覆盖我存储桶中的文件。

After uploading a file, assuming I have versioning enabled, is there a way to get the original version ID so I can store it in my database for retrieval later when displaying that file?上传文件后,假设我启用了版本控制,有没有办法获取原始版本 ID,以便我可以将其存储在我的数据库中,以便稍后在显示该文件时检索?

This is my current upload code:这是我当前的上传代码:

s3.upload(params).on('httpUploadProgress', function(evt) {
                var uploaded = Math.round(evt.loaded / evt.total * 100);
                console.log(`File uploaded: ${uploaded}%`);
            }).send(function(err, data) {
                if(err){
                    alert(err);
                } else {
                    alert('File is uploaded successfully!');
                    console.log(data);
                }
            });

I do not see any version ID in the data response since it seems to be in the response headers.我在数据响应中没有看到任何版本 ID,因为它似乎在响应标头中。 Is there a way to extract it from there?有没有办法从那里提取它?

You're right;你是对的; your code doesn't have access to the previous/original versionId.您的代码无权访问以前/原始的versionId.

I don't know your exact requirements, but I see two ways how to do this:我不知道您的确切要求,但我看到了两种方法:

  1. Check the file metadata before upload with s3.headObject which return data.VersionId .在使用返回data.VersionIds3.headObject upload之前检查文件元数据。 If the file doesn't exist, it doesn't matter;如果文件不存在,也没关系; otherwise, you can save the original version ID in DB.否则,您可以将原始版本 ID 保存在数据库中。

  2. Don't save the versions in the DB at all, and fetch them with listObjectVersions when the file is displayed from S3.根本不要将版本保存在数据库中,并在文件从 S3 显示时使用listObjectVersions获取它们。

I was able to retrieve the original version ID immediately after upload using the following code (it uploads the image and then grabs the version right after using listObjectVersions):我能够使用以下代码在上传后立即检索原始版本 ID(它上传图像,然后在使用 listObjectVersions 后立即获取版本):

s3.upload(params).on('httpUploadProgress', function(evt) {
                var uploaded = Math.round(evt.loaded / evt.total * 100);
                console.log(`File uploaded: ${uploaded}%`);
            }).send(function(err, data) {
                if(err){
                    alert(err);
                } else {
                    alert('File is uploaded successfully!');
                    console.log(data);
                    var params = {
                      Bucket: "mybucket", 
                      Prefix: "images/path-to-image.jpg"
                     };
                     s3.listObjectVersions(params, function(err, data) {
                       var versionId = data["Versions"][0]["VersionId"];
                       alert(versionId);
                   });
                }
            });

Be sure to have list object versions enabled in your bucket policy:请务必在您的存储桶策略中启用列表 object 版本:

{
        "Sid": "Statement2",
        "Effect": "Allow",
        "Principal": "*",
        "Action": "s3:ListBucketVersions",
        "Resource": "arn:aws:s3:::mybucket"
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM