S3 Presigned URL Issue - 文件上传成功,200 statusCode但没有响应正文

时间:2018-05-24 07:51:08

标签: javascript amazon-web-services amazon-s3

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

我无法理解我是否错过了某种标题,或者他们是否错了。响应标头上的Content-length让我担心。

非常感谢任何帮助!

注意:我已经模糊了这些值,如果它们有用,我可以在

中嘲笑它们

一般

Request URL: https://some-bucket.s3.ap-southeast-2.amazonaws.com/some/path/file/picture?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=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-read Request Method: PUT Status Code: 200 OK Remote Address: 1.2.3.4:443 Referrer Policy: no-referrer-when-downgrade

请求标头

Accept: application/json, text/plain, */* Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Cache-Control: no-cache Connection: keep-alive Content-Length: 31897 Content-Type: image/jpeg Host: some-bucket.s3.ap-southeast-2.amazonaws.com Origin: http://localhost:5000 Pragma: no-cache Referer: http://localhost:5000/some/page User-Agent: Mozilla/5.0 xxx

响应标头

Access-Control-Allow-Methods: GET, PUT, POST, HEAD Access-Control-Allow-Origin: * Content-Length: 0 Date: Thu, 1 May 2017 01:00:00 GMT ETag: "xxx" Server: AmazonS3 Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method x-amz-id-2: xxx x-amz-request-id: xxx

查询字符串参数

X-Amz-Algorithm: AWS4-HMAC-SHA256 X-Amz-Credential: ASIAXXXap-southeast-2/s3/aws4_request X-Amz-Date: XXX X-Amz-Expires: 300 X-Amz-Security-Token: XXX X-Amz-Signature: XXX X-Amz-SignedHeaders: host;x-amz-acl x-amz-acl: public-read

Presigning service(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!
    },
    () => {}
  );

1 个答案:

答案 0 :(得分:4)

我使用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