【问题标题】:Uploading file to S3 using a pre-signed URL使用预签名 URL 将文件上传到 s3
【发布时间】:2020-09-30 22:56:02
【问题描述】:

我正在尝试使用 aws 预签名 URL 在我的 s3 存储桶中上传文件。

这是我的js函数

function UploadObjectUsingPresignedURL() 
{
    var file = document.getElementById('customFile').files[0];
    console.log(file);
    var xhr = new XMLHttpRequest();
    xhr.open('PUT', 'hereMyPresignedURL', true);
    //xhr.setRequestHeader('Content-Type', 'image/jpeg');
    xhr.onload = () => {
      if (xhr.status === 200) {
        console.log('Uploaded data successfully');
      }
    };
    xhr.onerror = () => {
      console.log('Nope')
    };
    xhr.send(file); // `file` is a File object here 
}

这里是我的html

<div class="input-group">
                <div class="custom-file">
                    <input type="file" class="custom-file-input" id="customFile"
                        aria-describedby="customFile">
                    <label class="custom-file-label" for="customFile">Choose file</label>
                </div>
                <div class="input-group-append">
                    <button class="btn btn-outline-secondary" type="button" id="customFile" 
                    onclick="UploadObjectUsingPresignedURL()">Button</button>
                </div>
            </div>

这是我控制台中的结果...

console output

  • functions.js:4 文件 {name: "aragorn.jpg", lastModified: 1590136296908, lastModifiedDate: Fri May 22 10:31:36 GMT+0200 (heure d'été d'Europe centrale), webkitRelativePath: " ",尺寸:7714,...}
  • functions.js:10 上传数据成功)

看来一切顺利(上传数据成功),但在我的 S3 存储桶中我没有新文件...由于我没有错误,我不知道如何调试此...

谢谢你:) !!

编辑:这里是我的 .NET 代码,用于生成预签名 URL:

public static string MakeS3PresignedURIUpload()
        {
            string awsAccessKeyId = "XXX";
            string awsSecretAccessKey = "XXX";
            string s3Bucket = "test-bucket";
            string s3Key = "/aragorn.jpg";
            string awsRegion = "us-west-2";


            string presignedUri = "Error : No S3 URI found!";
            int expirationMinutes = 60;



            if (s3Bucket != String.Empty && s3Key != String.Empty && awsRegion != String.Empty)
            {
                try
                {
                    s3Key = s3Key.Replace("\\", "/");
                    GetPreSignedUrlRequest presignedUriReq = new GetPreSignedUrlRequest();
                    RegionEndpoint myRegion = RegionEndpoint.GetBySystemName(awsRegion);
                    AmazonS3Client client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, myRegion);
                    presignedUriReq.Verb = HttpVerb.PUT;
                    presignedUriReq.BucketName = s3Bucket;
                    presignedUriReq.Key = s3Key;
                    presignedUriReq.Expires = DateTime.UtcNow.AddMinutes(expirationMinutes);
                    presignedUri = client.GetPreSignedURL(presignedUriReq);
                }
                catch (AmazonS3Exception e)
                {
                    return string.Format("Error : S3 - MakeS3PresignedURIUpload - Error encountered on server.Message:'{0}' when writing an object", e.Message);
                }
                catch (Exception e)
                {
                    return string.Format("Error : S3 - MakeS3PresignedURIUpload - Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
                }
            }
            return presignedUri;
        }

【问题讨论】:

  • 也许还可以通过 console.log(xhr.responseText) 了解更多关于 aws 的响应?您是否使用 AWS S3 控制台查看文件?
  • @wschopohl 感谢您的评论 :) 是的,我使用 s3.console.aws.amazon.com 查看文件。我只是尝试 console.log(xhr.responseText) 和 console.log(xhr.response) 都是空的......这是什么意思?
  • 嗯,我认为这应该意味着它有效,就像你说的那样。你是如何生成 presignedUrl 的?我认为您需要在您的 presignedUrl 代码和您的 xml 请求中设置一个“Content-Type”请求标头..
  • @wschopohl 我在帖子中添加了生成预签名 URL 的功能。这只是为了测试,所以我只是在控制台中运行这个 .NET 项目,然后将返回的 URL 复制/粘贴到我的 JS 项目中。但是如果我添加一个Content-Type,我只能发送相同类型的文件吗?如果我想发送一个 jpg 文件,然后发送一个 pdf 文件?
  • 根据我的阅读,在您的 .NET 代码和 js 代码中设置 ContentType 很重要,不妨试一试!除此之外,我认为无论如何你都需要一个新的预签名 URL 来处理任何新的文件放置请求..

标签: javascript amazon-s3 upload xmlhttprequest pre-signed-url


【解决方案1】:
    string s3Key = "/aragorn.jpg";

改成

    string s3Key = "aragorn.jpg";

它正在工作,但将文件保存在存储桶中的未命名文件夹中,而不是直接存储在存储桶中...

感谢@wschopohl 帮助我 :) !

【讨论】:

    【解决方案2】:

    有用的链接:使用预签名 URL 将文件上传到 Amazon S3 存储桶

    Upload File to Amazon S3 Bucket Using Presigned URL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 2019-01-19
      • 2016-10-12
      • 2012-04-23
      • 1970-01-01
      • 2022-08-20
      相关资源
      最近更新 更多