gpt4 book ai didi

javascript - S3 预签名 URL 问题 - 文件上传成功,200 statusCode 但没有响应正文

转载 作者:可可西里 更新时间:2023-11-01 01:53:55 25 4
gpt4 key购买 nike

这直到最近才有效,我不确定发生了什么变化。我正在使用 S3 SDK 生成一个 presignedUrl 并使用它上传一个文件到一个存储桶。文件实际上传并且响应返回 statusCode 200,但奇怪的是没有响应正文。

我不明白我是否缺少某种标题,或者它们是否有误。响应 header 上的 Content-length 让我担心。

如有任何帮助,我们将不胜感激!

注意:我已经模糊了这些值,如果它们有帮助,我可以将它们模拟回来

一般

请求网址:https://some-bucket.s3.ap-southeast-2.amazonaws.com/some/path/file/picture?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz -凭据=ASIAXXXap-southeast-2%2Fs3%2Faws4_request&X-Amz-Date=xxx&X-Amz-Expires=300&X-Amz-Security-Token=xxx&X-Amz-Signature=xxx&X-Amz-SignedHeaders=host%3Bx-amz-acl&x -amz-acl=公共(public)阅读
请求方法:PUT
状态码:200 OK
远程地址:1.2.3.4:443
推荐人政策:降级时不推荐人

请求 header

接受:application/json, text/plain, */*
接受编码:gzip、deflate、br
接受语言:en-US,en;q=0.9
缓存控制:无缓存
连接:保持事件状态
内容长度:31897
内容类型:图像/jpeg
主机:some-bucket.s3.ap-southeast-2.amazonaws.com
来源:http://localhost:5000
Pragma:无缓存
推荐人:http://localhost:5000/some/page
用户代理:Mozilla/5.0 xxx

响应头

访问控制允许方法:GET、PUT、POST、HEAD
访问控制允许来源:*
内容长度:0
日期:2017 年 5 月 1 日星期四 01:00:00 GMT
电子标签:“xxx”
服务器:AmazonS3
变化:来源、访问控制请求 header 、访问控制请求方法
x-amz-id-2: xxx
x-amz-请求-id: xxx

查询字符串参数

X-Amz-算法:AWS4-HMAC-SHA256
X-Amz-凭证:ASIAXXXap-southeast-2/s3/aws4_request
X-Amz-日期:XXX
X-Amz-过期:300
X-Amz-安全 token :XXX
X-Amz-签名:XXX
X-Amz-SignedHeaders:主机;x-amz-acl
x-amz-acl:公开阅读

预签名服务(NodeJS、Lambda)

... 
const params = {
Bucket: 'some-bucket',
Key: 'some/path/file/picture',
Expires: 60 * 5,
ACL: 'public-read'
};

s3.getSignedUrl('putObject', params, (err, url) => {
...
callback(null, new Response(200, {url});
});
...

S3 服务(TS、Angular)

public putObject(presignedUrl: string, file: File): Observable<any> {
return this.http.put<any>(presignedUrl, file);
}

组件

this.s3Service.putObject(presignedUrl, file)
.subscribe(
(response) => {
// it gets here as expected
// but response is null!
},
() => {}
);

最佳答案

我使用 go SDK 重现了它,以确认它是 API 本身的行为方式,而不是 nodejs 的特定行为。

从我的实验中可以看出,它现在是一种正常行为,它不会从 PUT 请求返回任何内容。

OBS:我屏蔽了一些敏感值!

代码:

package main

import (
"crypto/tls"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)

func main() {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("eu-west-1")},
)

svc := s3.New(sess)

req1, _ := svc.PutObjectRequest(&s3.PutObjectInput{
Bucket: aws.String("bucketversioningenabled"),
Key: aws.String("myKey"),
Body: strings.NewReader("EXPECTED CONTENTS"),
})

presignURL, err := req1.Presign(time.Minute * 1)
if err != nil {
log.Println("Error on presign", err)
return
}
fmt.Println("Presign: ", presignURL, err)
fmt.Println("")

req2, err := http.NewRequest("PUT", presignURL, strings.NewReader("EXPECTED CONTENTS"))
if err != nil {
log.Println("error creating request", err)
return
}
fmt.Println("NewRequest: ", req2, err)
fmt.Println("")

proxyURL, err := url.Parse("http://myfiddler.proxy.com:8888")
if err != nil {
log.Println("Error on proxy parse", err)
return
}

tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
Proxy: http.ProxyURL(proxyURL),
}
client := &http.Client{
Transport: tr,
Timeout: time.Duration(5 * time.Second),
}

resp, err := client.Do(req2)
if err != nil {
log.Println("error on request put", err)
return
}
fmt.Println("Do: ", resp, err)
}

执行输出:

请看最后一行的Content-Length:[0]

$ ./s3put 
Presign: https://bucketversioningenabled.s3.eu-west-1.amazonaws.com/myKey?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=XXXXX%2F20180531%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20180531T112707Z&X-Amz-Expires=60&X-Amz-Security-Token=XXXX&X-Amz-SignedHeaders=content-length%3Bhost&X-Amz-Signature=XXXX <nil>

NewRequest: &{PUT https://bucketversioningenabled.s3.eu-west-1.amazonaws.com/myKey?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=XXXX%2F20180531%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20180531T112707Z&X-Amz-Expires=60&X-Amz-Security-Token=XXXX&X-Amz-SignedHeaders=content-length%3Bhost&X-Amz-Signature=XXXX HTTP/1.1 1 1 map[] {0xc42010d020} 0x5f2040 17 [] false bucketversioningenabled.s3.eu-west-1.amazonaws.com map[] map[] <nil> map[] <nil> <nil> <nil> <nil>} <nil>

Do: &{200 OK 200 HTTP/1.1 1 1 map[X-Amz-Version-Id:[3I4txVUgi4ObULr8EVadA4U3cfvdVwQM] Etag:["952973475e3f4d992fe48578086c1e17"] Content-Length:[0] Server:[AmazonS3] X-Amz-Id-2:[yGUZtjttGKwv0uJxQcG7bIkGRqxhPxKeW71jWIGkmwt73oZY/+r3HWyr2uK07nR8xTDQyzbM3Hw=] X-Amz-Request-Id:[509F1785D0383ADA] Date:[Thu, 31 May 2018 11:27:09 GMT]] 0xc42000c0e0 0 [] false false map[] 0xc42019c400 0xc420199290} <nil>

下面是Fiddler的请求和响应

要求:

PUT https://bucketversioningenabled.s3.eu-west-1.amazonaws.com/myKey?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=XXXX%2F20180531%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20180531T112707Z&X-Amz-Expires=60&X-Amz-Security-Token=XXXX&X-Amz-SignedHeaders=content-length%3Bhost&X-Amz-Signature=XXXX HTTP/1.1
Host: bucketversioningenabled.s3.eu-west-1.amazonaws.com
User-Agent: Go-http-client/1.1
Content-Length: 17
Accept-Encoding: gzip

EXPECTED CONTENTS

响应(查看内容长度):

HTTP/1.1 200 OK
x-amz-id-2: yGUZtjttGKwv0uJxQcG7bIkGRqxhPxKeW71jWIGkmwt73oZY/+r3HWyr2uK07nR8xTDQyzbM3Hw=
x-amz-request-id: 509F1785D0383ADA
Date: Thu, 31 May 2018 11:27:09 GMT
x-amz-version-id: 3I4txVUgi4ObULr8EVadA4U3cfvdVwQM
ETag: "952973475e3f4d992fe48578086c1e17"
Content-Length: 0
Server: AmazonS3

关于javascript - S3 预签名 URL 问题 - 文件上传成功,200 statusCode 但没有响应正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50503855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com