能够列出存储桶但无法上传文件。

huangapple go评论100阅读模式
英文:

Able to list Bucket but cannot upload a file

问题

使用 ASW JavaScript SDK v2(由于与我的项目相关的依赖关系,无法使用 v3),当我尝试使用以下 JS 代码将文件上传到 S3 存储桶时,我遇到了一个错误。

FYI:

  1. 我可以在 VScode 命令行中通过 aws CLI 成功执行此操作。
  2. 我的配置文件已配置,并且我可以使用代码列出存储桶。

以下是我的代码:

const fs = require('fs');
const AWS = require('aws-sdk');

let s3 = new AWS.S3({apiVersion: '2006-03-01'});

const fileName = '/Users/*****/****/*****-ui/**/***/****/BE-Commands/cmd.json';

const uploadFile = () => {
  fs.readFile(fileName, (err, data) => {
     if (err) throw err;
     const params = {
         Bucket: 'bucket-test', // 传入您的存储桶名称
         Key: 'contacts.csv', // 文件将保存为 testBucket/contacts.csv
         Body: JSON.stringify(data, null, 2)
     };
     s3.upload(params, function(s3Err, data) {
         if (s3Err) throw s3Err
         console.log(`文件成功上传至 ${data.Location}`)
     });
  });
};

uploadFile();

遇到以下错误:

Error: connect EHOSTDOWN 169.254.169.254:80 - 本地 (***.1***.1.182:*****)
    at internalConnect (node:net:1084:16)
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)
    at node:net:1290:9
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  message: '配置中缺少凭证,请设置 AWS_SDK_LOAD_CONFIG=1(如果使用 AWS_CONFIG_FILE)',
  errno: -64,
  code: 'CredentialsError',
  syscall: 'connect',
  address: '1****.254.169.254',
  port: 80,
  time: 2023-08-10T18:15:50.727Z,
  originalError: {
    message: '无法从任何提供程序加载凭据',
    errno: -64,
    code: 'CredentialsError',
    syscall: 'connect',
    address: '**********',
    port: 80,
    time: 2023-08-10T18:15:50.722Z,
    originalError: {
      message: 'EC2 Metadata roleName 请求返回错误',
      errno: -64,
      code: 'EHOSTDOWN',
      syscall: 'connect',
      address: '***.254.***.***',
      port: 80,
      time: 2023-08-10T18:15:50.722Z,
      originalError: {
        errno: -64,
        code: 'EHOSTDOWN',
        syscall: 'connect',
        address: '***.***.***.***',
        port: 80,
        message: 'connect EHOSTDOWN 169.254.169.254:80 - 本地 (***.***.1.***:49426)'
      }
    }
  }
}

希望这能帮助您解决问题。如果需要更多帮助,请随时提问。

英文:

Using ASW javascript SDK v2 (cannot use v3 for dependency reasons related to my project)
I am facing an error when trying to upload a file to an S3 bucket with the following JS code.

FYI :

  1. I am able to do so in VScode command line through aws CLI
  2. My profile is configured and I can list buckets with code

Here is my code :

const fs = require('fs');
const AWS = require('aws-sdk');

let s3 = new AWS.S3({apiVersion: '2006-03-01'});

const fileName = '/Users/*****/****/*****-ui/**/***/****/BE-Commands/cmd.json';

const uploadFile = () => {
  fs.readFile(fileName, (err, data) => {
     if (err) throw err;
     const params = {
         Bucket: 'bucket-test', // pass your bucket name
         Key: 'contacts.csv', // file will be saved as testBucket/contacts.csv
         Body: JSON.stringify(data, null, 2)
     };
     s3.upload(params, function(s3Err, data) {
         if (s3Err) throw s3Err
         console.log(`File uploaded successfully at ${data.Location}`)
     });
  });
};

uploadFile();

Getting this error :

Error: connect EHOSTDOWN 169.254.169.254:80 - Local (***.1***.1.182:*****)
    at internalConnect (node:net:1084:16)
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)
    at node:net:1290:9
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
  errno: -64,
  code: 'CredentialsError',
  syscall: 'connect',
  address: '1****.254.169.254',
  port: 80,
  time: 2023-08-10T18:15:50.727Z,
  originalError: {
    message: 'Could not load credentials from any providers',
    errno: -64,
    code: 'CredentialsError',
    syscall: 'connect',
    address: '**********',
    port: 80,
    time: 2023-08-10T18:15:50.722Z,
    originalError: {
      message: 'EC2 Metadata roleName request returned error',
      errno: -64,
      code: 'EHOSTDOWN',
      syscall: 'connect',
      address: '***.254.***.***',
      port: 80,
      time: 2023-08-10T18:15:50.722Z,
      originalError: {
        errno: -64,
        code: 'EHOSTDOWN',
        syscall: 'connect',
        address: '***.***.***.***',
        port: 80,
        message: 'connect EHOSTDOWN 169.254.169.254:80 - Local (***.***.1.***:49426)'
      }
    }
  }

答案1

得分: 0

The SDK for AWS is detecting your environment. 所以您无法上传文件的原因是因为您的配置不正确/未被检测到。请确保您正确传递了配置凭据。

链接:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html

编辑:由于OP正在使用SDK v2,已更新SDK v2的文档。

英文:

Well, the SDK for AWS is detecting your environment. So the reason that you're not able to upload your file is because your configurations are not correct/not being detected. Ensure, that you are passing your configuration credentials correctly.

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html

EDIT: Updated Documentation for SDK v2 since OP is using it

答案2

得分: 0

这与我在浏览器中运行它有关,所以代码没有起作用。我只是将代码添加到外部JS代码中,然后它就可以工作了。

英文:

It turned out to be related to the fact that I am running it in a browser so the code did not work. I have simply added the code in an external JS code and it worked

huangapple
  • 本文由 发表于 2023年8月11日 02:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定