通过nginx连接AWS S3

gblwokeq  于 2023-04-05  发布在  Nginx
关注(0)|答案(1)|浏览(324)

我已经创建了一个java客户端,使用AWS SDK将文件上传到S3桶。它工作得非常好。
现在我正在尝试在中间使用Nginx做同样的事情。因为所有来自我客户端的请求都将通过Nginx。
我刚接触Nginx,所以编译了这个nginx.conf

events {}
http {

  server {
    listen 9091;

    location / {
      proxy_pass http://bucket-name.s3.ap-south-1.amazonaws.com/;
    }
  }
}

此配置给出以下例外:

Caused by: com.amazonaws.services.s3.model.AmazonS3Exception: The request signature we calculated does not match the signature you provided. Check your key and signing method. (Service: Amazon S3; Status Code: 403; Error Code: SignatureDoesNotMatch; Request ID: 7BB1301CCDE06B05; S3 Extended Request ID: qAVh3RMieyqgwOrzNeiMAVQyb9V59J0rThHYqnfzUibZxqDIzzMUPI8SdWLCa9tL15YyekI2ukQ=), S3 Extended Request ID: qAVh3RMieyqgwOrzNeiMAVQyb9V59J0rThHYqnfzUibZxqDIzzMUPI8SdWLCa9tL15YyekI2ukQ=
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1799)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleServiceErrorResponse(AmazonHttpClient.java:1383)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1359)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1139)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:796)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:764)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:738)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:698)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:680)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:544)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:524)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:5052)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4998)
    at com.amazonaws.services.s3.AmazonS3Client.initiateMultipartUpload(AmazonS3Client.java:3574)... 19 more

我试图调整配置,但没有成功。

lkaoscv7

lkaoscv71#

您可以尝试设置客户端最初使用的Host头(Nginx默认会重写此头),因为AWS签名也是从该字段计算的。
Nginx配置可能看起来像这样(注意添加的proxy_set_header行):

events {}
http {

  server {
    listen 9091;

    location / {
      proxy_set_header       Host $http_host;
      proxy_pass http://bucket-name.s3.ap-south-1.amazonaws.com/;
    }
  }
}

这是基于另一个StackOverflow问题的以下答案:Nginx confg issue - couldn't connect to S3 compatible storage from NodeJS test program

相关问题