使用预签名URL将文件上传到s3 [英] Uploading file to s3 using presigned-URL

查看:380
本文介绍了使用预签名URL将文件上传到s3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将a3带有预签名URL的文件上传到我的s3存储桶中.

I'm trying to upload a file in my s3 bucket with a aws presigned-URL.

这是我的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>

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

And here the result in my console ...

控制台输出

  • functions.js:4文件{name:"aragorn.jpg",lastModified:1590136296908,lastModifiedDate:2020年5月22日星期五10:31:36 GMT + 0200(欧洲中央大街),webkitRelativePath:",大小:7714,...}
  • functions.js:10成功上传数据)

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

So it seems it went well (Uploaded data successfully), but in my S3 bucket I have no new files ... Since I have no error,I don't know how to debug this ...

谢谢:) !!

这是我的.NET代码,用于生成Presigned-URL:

EDIT :Here my .NET code for generate the Presigned-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;
        }

推荐答案

    string s3Key = "/aragorn.jpg";

更改为

    string s3Key = "aragorn.jpg";

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

It was working, but saving the file in an un-named folder in the bucket and not directly in the bucket ...

感谢@wschopohl帮助我:)!

Thanks you @wschopohl for helping me :) !

这篇关于使用预签名URL将文件上传到s3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆