通过 curl 路由上传到 Amazon S3 [英] Uploading to Amazon S3 via curl route
问题描述
我正在尝试通过 Spring Boot 设置文件上传 REST API.
I am trying to set up a file upload REST API via Spring Boot.
我目前有一个 list/GET 方法 curl http://localhost:8080/api/aws/s3/list
返回当前存在于存储桶中的对象列表.
I currently have a list/GET method curl http://localhost:8080/api/aws/s3/list
which returns a list of objects that currently exist in the bucket.
对于上传,我一直在尝试:
For upload, I have been trying:
curl -F "data=@test.txt" http://localhost:8080/api/aws/s3/upload -i
产生:
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Sun, 25 Jun 2017 23:28:36 GMT
X-Application-Context: application
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
但是当我查看存储桶时,它尚未使用新文件进行更新.
But when I look at the bucket, it hasn't been updated with the new file.
这会是 AWS 上的权限问题吗?只有我的帐户具有读写权限.我创建的用户和组具有管理员权限.我没有添加存储桶策略.这是我的 CORS 配置:
Would this be a permissions issue on AWS? Only my account has read and write access. The user and group I created have admin privileges. I didn't add a Bucket Policy. This is my CORS configuration:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
这是我的 S3 控制器在 spring 的上传部分:
Here is the upload section of my S3 Controller in spring:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public List<PutObjectResult> upload(@RequestParam("file") MultipartFile[] multipartFiles) {
return s3Wrapper.upload(multipartFiles);
}
推荐答案
您应该为 S3 指定您使用的存储桶名称和一些参数.我猜它是 PUT,而不是 POST.网上有几个命令行示例.
You should specify your using bucket name and some parameters for S3. And I guess it's PUT, not POST. There are several command line samples in the internet.
file=/path/to/file/to/upload.tar.gz
bucket=your-bucket
resource="/${bucket}/${file}"
contentType="application/x-compressed-tar"
dateValue=`date -R`
stringToSign="PUT
${contentType}
${dateValue}
${resource}"
s3Key=xxxxxxxxxxxxxxxxxxxx
s3Secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
curl -X PUT -T "${file}"
-H "Host: ${bucket}.s3.amazonaws.com"
-H "Date: ${dateValue}"
-H "Content-Type: ${contentType}"
-H "Authorization: AWS ${s3Key}:${signature}"
https://${bucket}.s3.amazonaws.com/${file}
- 使用 18 行 Shell 上传到 S3(用于上传 http://soltrader.net 的构建)
- 在 Bash 中上传到 S3
这篇关于通过 curl 路由上传到 Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!