AWS 在使用 uploadUrl 时将元数据添加到文件内容
AWS adds metadata to file content when using uploadUrl
我正在使用下一个代码通过 uploadUrl 获取上传说明:
public UploadInstruction getUploadUrl() {
final var objectKey = getObjectKey(generateUniqueFileKey());
final var url = amazonS3.generatePresignedUrl(s3Properties.getBucket(), objectKey,
calculateExpirationDate(s3Properties.getDownload().getUrlExpiration()), HttpMethod.PUT);
return new UploadInstruction(url, getFileKey(objectKey));
}
我通过 api 获得 URL 并使用 Postman 发送 PUT 请求。但是当我使用 URL 上传文件时,我发现 AWS 在我的文件开头添加了一些元数据,例如 "Content-type: Application octets stream...."。这是一个错误吗?如何避免使用 was upload URL?
向上传的文件添加额外信息
更新:
例如,我想上传带有以下文本的简单 txt 文件:
test
当我通过 uploadUrl 上传到 S3 时,下载后我得到一个包含以下内容的文件:
----------------------------174475527638909501568708
Content-Disposition: form-data; name=""; filename="test.txt"
Content-Type: text/plain
test
----------------------------174475527638909501568708--
另外,我在生成uploadUrl的时候改了文件名。此外,当我上传 zip 存档时,我无法在从 s3 存储桶下载后解压缩它。但是当我通过 AWS CLI 上传文件时一切正常。
PUT请求中的地址为uploadUrl。我的headers哪个邮递员添加的是下一个:
Amazon 总是将某些元数据添加到您的 S3 对象。可以找到它们的完整列表 here。根据亚马逊 There are two kinds of metadata: system metadata and user-defined metadata
。自动添加某些元数据属性,例如内容类型。
- Metadata such as object creation date is system controlled, where only Amazon S3 can modify the value.
- Other system metadata, such as the storage class configured for the object and whether the object has server-side encryption enabled, are examples of system metadata whose values you control. If your bucket is configured as a website, sometimes you might want to redirect a page request to another page or an external URL. In this case, a webpage is an object in your bucket. Amazon S3 stores the page redirect value as system metadata whose value you control.
我找到了一些信息here。
通常在上传文件时使用 form-data:
var fd = new FormData();
var file = document.getElementById('file')[0];
fd.append('file',file);
接下来需要用到:
var upload = document.getElementById('file');
var file = upload.files[0];
完整代码:
<form method="put" action="" enctype="multipart/form-data" id="myform">
<div >
<input type="file" id="file" name="file" />
<input type="button" class="button" value="Upload" id="but_upload">
</div>
</form>
Ajax:
$(document).ready(function(){
$("#but_upload").click(function(){
var upload = document.getElementById('file');
var file = upload.files[0];
$.ajax({
url: '<uploadURL>',
type: 'put',
data: file,
contentType: false,
processData: false,
success: function(response){
if(response != 0){
alert('file uploaded');
}else{
alert('file not uploaded');
}
},
});
});
});
在 Postman 中,必须使用二进制而不是表单数据来附加文件:
我正在使用下一个代码通过 uploadUrl 获取上传说明:
public UploadInstruction getUploadUrl() {
final var objectKey = getObjectKey(generateUniqueFileKey());
final var url = amazonS3.generatePresignedUrl(s3Properties.getBucket(), objectKey,
calculateExpirationDate(s3Properties.getDownload().getUrlExpiration()), HttpMethod.PUT);
return new UploadInstruction(url, getFileKey(objectKey));
}
我通过 api 获得 URL 并使用 Postman 发送 PUT 请求。但是当我使用 URL 上传文件时,我发现 AWS 在我的文件开头添加了一些元数据,例如 "Content-type: Application octets stream...."。这是一个错误吗?如何避免使用 was upload URL?
向上传的文件添加额外信息更新: 例如,我想上传带有以下文本的简单 txt 文件:
test
当我通过 uploadUrl 上传到 S3 时,下载后我得到一个包含以下内容的文件:
----------------------------174475527638909501568708
Content-Disposition: form-data; name=""; filename="test.txt"
Content-Type: text/plain
test
----------------------------174475527638909501568708--
另外,我在生成uploadUrl的时候改了文件名。此外,当我上传 zip 存档时,我无法在从 s3 存储桶下载后解压缩它。但是当我通过 AWS CLI 上传文件时一切正常。
PUT请求中的地址为uploadUrl。我的headers哪个邮递员添加的是下一个:
Amazon 总是将某些元数据添加到您的 S3 对象。可以找到它们的完整列表 here。根据亚马逊 There are two kinds of metadata: system metadata and user-defined metadata
。自动添加某些元数据属性,例如内容类型。
- Metadata such as object creation date is system controlled, where only Amazon S3 can modify the value.
- Other system metadata, such as the storage class configured for the object and whether the object has server-side encryption enabled, are examples of system metadata whose values you control. If your bucket is configured as a website, sometimes you might want to redirect a page request to another page or an external URL. In this case, a webpage is an object in your bucket. Amazon S3 stores the page redirect value as system metadata whose value you control.
我找到了一些信息here。 通常在上传文件时使用 form-data:
var fd = new FormData();
var file = document.getElementById('file')[0];
fd.append('file',file);
接下来需要用到:
var upload = document.getElementById('file');
var file = upload.files[0];
完整代码:
<form method="put" action="" enctype="multipart/form-data" id="myform">
<div >
<input type="file" id="file" name="file" />
<input type="button" class="button" value="Upload" id="but_upload">
</div>
</form>
Ajax:
$(document).ready(function(){
$("#but_upload").click(function(){
var upload = document.getElementById('file');
var file = upload.files[0];
$.ajax({
url: '<uploadURL>',
type: 'put',
data: file,
contentType: false,
processData: false,
success: function(response){
if(response != 0){
alert('file uploaded');
}else{
alert('file not uploaded');
}
},
});
});
});
在 Postman 中,必须使用二进制而不是表单数据来附加文件: