最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

AWS

互联网 admin 56浏览 0评论

AWS

当我尝试在 nodejs 中使用 aws-sdk-s3-client 上传文件时。它抛出验证错误:

错误:解析的凭证对象无效 在 SignatureV4.validateResolvedCredentials (/api/node_modules/@aws-sdk/signature-v4/dist-cjs/SignatureV4.js:163:19) 在 SignatureV4.signRequest (/api/node_modules/@aws-sdk/signature-v4/dist-cjs/SignatureV4.js:88:14)

这是我的 s3Client 配置文件的样子:

s3_util.js

const dotenv = require("dotenv");
dotenv.config();
const AWS = require("@aws-sdk/client-s3");

const bucketRegion = process.env.BUCKET_REGION;
const accessKey = process.env.ACCESS_KEY;
const secretAccesskey = process.env.SECRET_ACCESS_KEY;

const s3 = new AWS.S3Client({
  credentials: {
    accessKeyId: accessKey,
    accessKey: secretAccesskey,
  },
  region: bucketRegion,
});

module.exports = s3;

上传文件到 s3

const { PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
const s3 = require("./s3_util");
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");

const uploadFileToS3 = async (bucketName, file, uploadPath) => {
  console.log(file, "file");
  const commandProps = {
    Bucket: bucketName,
    Key: uploadPath + file.originalname,
    Body: file.buffer,
    ContentType: file.mimetype,
  };

  const uploadFileCommand = new PutObjectCommand(commandProps);
  let data;
  try {
    data = await s3.send(uploadFileCommand);
  } catch (error) {
    console.log(error);
    // const { requestId, cfId, extendedRequestId } = error.$$metadata;
    // console.log({ requestId, cfId, extendedRequestId });
  }
  return data;
};

我想要解决它为什么会抛出错误的解决方案,以及解决该错误所涉及的设置是什么。

回答如下:

确保导入 dotenv 模块

import * as dotenv from 'dotenv'; 
dotenv.config();
const s3Client = new S3Client({
  endpoint: 'https://sgp1.digitaloceanspaces',
  forcePathStyle: false,
  region: 'sgp1',
  credentials: {
    accessKeyId: process.env.YOUR_ACCESS_KEY,
    secretAccessKey: process.env.YOUR_SECRET_KEY,
  }
});

这是一个简单的代码快照,因为我使用的是数字海洋桶,它看起来像这样并且可以正常工作。

AWS

当我尝试在 nodejs 中使用 aws-sdk-s3-client 上传文件时。它抛出验证错误:

错误:解析的凭证对象无效 在 SignatureV4.validateResolvedCredentials (/api/node_modules/@aws-sdk/signature-v4/dist-cjs/SignatureV4.js:163:19) 在 SignatureV4.signRequest (/api/node_modules/@aws-sdk/signature-v4/dist-cjs/SignatureV4.js:88:14)

这是我的 s3Client 配置文件的样子:

s3_util.js

const dotenv = require("dotenv");
dotenv.config();
const AWS = require("@aws-sdk/client-s3");

const bucketRegion = process.env.BUCKET_REGION;
const accessKey = process.env.ACCESS_KEY;
const secretAccesskey = process.env.SECRET_ACCESS_KEY;

const s3 = new AWS.S3Client({
  credentials: {
    accessKeyId: accessKey,
    accessKey: secretAccesskey,
  },
  region: bucketRegion,
});

module.exports = s3;

上传文件到 s3

const { PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
const s3 = require("./s3_util");
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");

const uploadFileToS3 = async (bucketName, file, uploadPath) => {
  console.log(file, "file");
  const commandProps = {
    Bucket: bucketName,
    Key: uploadPath + file.originalname,
    Body: file.buffer,
    ContentType: file.mimetype,
  };

  const uploadFileCommand = new PutObjectCommand(commandProps);
  let data;
  try {
    data = await s3.send(uploadFileCommand);
  } catch (error) {
    console.log(error);
    // const { requestId, cfId, extendedRequestId } = error.$$metadata;
    // console.log({ requestId, cfId, extendedRequestId });
  }
  return data;
};

我想要解决它为什么会抛出错误的解决方案,以及解决该错误所涉及的设置是什么。

回答如下:

确保导入 dotenv 模块

import * as dotenv from 'dotenv'; 
dotenv.config();
const s3Client = new S3Client({
  endpoint: 'https://sgp1.digitaloceanspaces',
  forcePathStyle: false,
  region: 'sgp1',
  credentials: {
    accessKeyId: process.env.YOUR_ACCESS_KEY,
    secretAccessKey: process.env.YOUR_SECRET_KEY,
  }
});

这是一个简单的代码快照,因为我使用的是数字海洋桶,它看起来像这样并且可以正常工作。

发布评论

评论列表 (0)

  1. 暂无评论