javascript 如何上传文件到山葵桶与API与Node.js?

fcg9iug3  于 2023-02-07  发布在  Java
关注(0)|答案(2)|浏览(151)

我实现了上传文件在亚马逊S3桶如下,它的工作很好:

const S3 = require('aws-sdk/clients/s3');
  const AWS = require('aws-sdk');

  const accessKeyId = 'AKIAYVXDX*******';
  const secretAccessKey = 'gxZpdSDnOfpM*****************';

  const s3 = new S3({
    region: 'us-east-1',
    accessKeyId,
    secretAccessKey
  });

  s3.putObject({
      Body: 'Hello World',
      Bucket: "dev-amazon",
      Key: 'hello.txt'
    }
    , (err, data) => {
      if (err) {
         console.log(err);
      }
    });

我需要实现上传文件在山葵桶。
我尝试如下:

const S3 = require('aws-sdk/clients/s3');
  const AWS = require('aws-sdk');
  const wasabiEndpoint = new AWS.Endpoint('s3.wasabisys.com');

  const accessKeyId = 'PEIL4DYOY*******';
  const secretAccessKey = 'D4jIz3tjJw*****************';

  const s3 = new S3({
    endpoint: wasabiEndpoint,
    region: 'us-east-2',
    accessKeyId,
    secretAccessKey
  });

  s3.putObject({
      Body: 'Hello World',
      Bucket: "dev-wasabi",
      Key: 'hello.txt'
    }
    , (err, data) => {
      if (err) {
         console.log(err);
      }
    });

而'console. log(err)的结果是:
err {" message ":"我们计算的请求签名与您提供的签名不匹配。请检查您的密钥和签名方法。"," code ":" SignatureDoesNotMatch "," region ":null," time ":" 2019 - 10 - 30T09:39:19.072Z "," requestId ":null," statusCode ":403," retryable ":false," retryDelay ":64.72166771381391}
devtools中的控制台错误:
放置https://dev-wasabi.s3.us-east-2.wasabisys.com/5efa9b286821fab7df3ece8dc3d6687ed32 403(禁止)
我的代码有什么问题?

djmepvbi

djmepvbi1#

经过一番研究,我发现wasabiEndpoint是错误的。
应该是

const wasabiEndpoint = new AWS.Endpoint('s3.us-east-2.wasabisys.com ');

根据docs,服务URL应该基于区域而不同。
Wasabi美国东部1(北弗吉尼亚州): 或s3.wasabisys.com or s3.us-east-1.wasabisys.com
Wasabi美国东部2(北弗吉尼亚州):www.example.coms3.us-east-2.wasabisys.com
Wasabi美国西部1(俄勒冈州):www.example.coms3.us-west-1.wasabisys.com
Wasabi EU Central 1(阿姆斯特丹):www.example.coms3.eu-central-1.wasabisys.com
如果这能帮助到某人,我会非常高兴的。)

ymzxtsji

ymzxtsji2#

当使用@aws-sdk/client-s3包时,S3客户端只需要定义Wasabi端点。下面将创建具有正确端点的S3客户端:

const client = new S3Client({
    credentials: {
        accessKeyId: "<wasabi-access-key-id>",
        secretAccessKey: "<wasabi-secret-key>"
    },
    endpoint: {
        url: "https://s3.wasabisys.com"
    }
})

从这里开始,放置对象与标准AWS S3存储桶完全相同。例如:

await client.send(new PutObjectCommand({
    Bucket: "bucket-name",
    Key: "object-key",
    Body: <whatever is being put>
})

基本导入语句为:

import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"

相关问题