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

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

Able to list Bucket but cannot upload a file

问题

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

FYI:

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

以下是我的代码:

  1. const fs = require('fs');
  2. const AWS = require('aws-sdk');
  3. let s3 = new AWS.S3({apiVersion: '2006-03-01'});
  4. const fileName = '/Users/*****/****/*****-ui/**/***/****/BE-Commands/cmd.json';
  5. const uploadFile = () => {
  6. fs.readFile(fileName, (err, data) => {
  7. if (err) throw err;
  8. const params = {
  9. Bucket: 'bucket-test', // 传入您的存储桶名称
  10. Key: 'contacts.csv', // 文件将保存为 testBucket/contacts.csv
  11. Body: JSON.stringify(data, null, 2)
  12. };
  13. s3.upload(params, function(s3Err, data) {
  14. if (s3Err) throw s3Err
  15. console.log(`文件成功上传至 ${data.Location}`)
  16. });
  17. });
  18. };
  19. uploadFile();

遇到以下错误:

  1. Error: connect EHOSTDOWN 169.254.169.254:80 - 本地 (***.1***.1.182:*****)
  2. at internalConnect (node:net:1084:16)
  3. at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)
  4. at node:net:1290:9
  5. at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  6. message: '配置中缺少凭证,请设置 AWS_SDK_LOAD_CONFIG=1(如果使用 AWS_CONFIG_FILE)',
  7. errno: -64,
  8. code: 'CredentialsError',
  9. syscall: 'connect',
  10. address: '1****.254.169.254',
  11. port: 80,
  12. time: 2023-08-10T18:15:50.727Z,
  13. originalError: {
  14. message: '无法从任何提供程序加载凭据',
  15. errno: -64,
  16. code: 'CredentialsError',
  17. syscall: 'connect',
  18. address: '**********',
  19. port: 80,
  20. time: 2023-08-10T18:15:50.722Z,
  21. originalError: {
  22. message: 'EC2 Metadata roleName 请求返回错误',
  23. errno: -64,
  24. code: 'EHOSTDOWN',
  25. syscall: 'connect',
  26. address: '***.254.***.***',
  27. port: 80,
  28. time: 2023-08-10T18:15:50.722Z,
  29. originalError: {
  30. errno: -64,
  31. code: 'EHOSTDOWN',
  32. syscall: 'connect',
  33. address: '***.***.***.***',
  34. port: 80,
  35. message: 'connect EHOSTDOWN 169.254.169.254:80 - 本地 (***.***.1.***:49426)'
  36. }
  37. }
  38. }
  39. }

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

英文:

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 :

  1. const fs = require('fs');
  2. const AWS = require('aws-sdk');
  3. let s3 = new AWS.S3({apiVersion: '2006-03-01'});
  4. const fileName = '/Users/*****/****/*****-ui/**/***/****/BE-Commands/cmd.json';
  5. const uploadFile = () => {
  6. fs.readFile(fileName, (err, data) => {
  7. if (err) throw err;
  8. const params = {
  9. Bucket: 'bucket-test', // pass your bucket name
  10. Key: 'contacts.csv', // file will be saved as testBucket/contacts.csv
  11. Body: JSON.stringify(data, null, 2)
  12. };
  13. s3.upload(params, function(s3Err, data) {
  14. if (s3Err) throw s3Err
  15. console.log(`File uploaded successfully at ${data.Location}`)
  16. });
  17. });
  18. };
  19. uploadFile();

Getting this error :

  1. Error: connect EHOSTDOWN 169.254.169.254:80 - Local (***.1***.1.182:*****)
  2. at internalConnect (node:net:1084:16)
  3. at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)
  4. at node:net:1290:9
  5. at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  6. message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
  7. errno: -64,
  8. code: 'CredentialsError',
  9. syscall: 'connect',
  10. address: '1****.254.169.254',
  11. port: 80,
  12. time: 2023-08-10T18:15:50.727Z,
  13. originalError: {
  14. message: 'Could not load credentials from any providers',
  15. errno: -64,
  16. code: 'CredentialsError',
  17. syscall: 'connect',
  18. address: '**********',
  19. port: 80,
  20. time: 2023-08-10T18:15:50.722Z,
  21. originalError: {
  22. message: 'EC2 Metadata roleName request returned error',
  23. errno: -64,
  24. code: 'EHOSTDOWN',
  25. syscall: 'connect',
  26. address: '***.254.***.***',
  27. port: 80,
  28. time: 2023-08-10T18:15:50.722Z,
  29. originalError: {
  30. errno: -64,
  31. code: 'EHOSTDOWN',
  32. syscall: 'connect',
  33. address: '***.***.***.***',
  34. port: 80,
  35. message: 'connect EHOSTDOWN 169.254.169.254:80 - Local (***.***.1.***:49426)'
  36. }
  37. }
  38. }

答案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:

确定