我正在尝试使用 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>
这是我控制台中的结果......
所以看起来很顺利(成功上传数据),但是在我的 S3 存储桶中我没有新文件......因为我没有错误,我不知道如何调试这个......
谢谢 :) !!
编辑:这里是我的 .NET 代码,用于生成 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";
它正在工作,但将文件保存在存储桶中的未命名文件夹中,而不是直接保存在存储桶中......
谢谢@wschopohl 帮助我:)!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句