繁体   English   中英

net::ERR_NAME_NOT_RESOLVED 将图片上传到 s3 存储桶

[英]net::ERR_NAME_NOT_RESOLVED while uploading picture to s3 bucket

我正在使用 angular 创建我的应用程序。 我正在尝试将图片上传到 s3 存储桶,但每次尝试上传时,此错误都会显示在我的控制台中。

在此处输入图像描述

这是我的 upload.service.ts 文件代码

  uploadBarcode(file: any) {
    const uniqueId: number = Math.floor(Math.random() * Date.now());
    const file_name: string = 'lab_consignments/'+uniqueId + "_" + file.name ;
    const contentType = file.type;
    console.log(contentType)
    const bucket = new S3(
      {
        accessKeyId: myAccessKey,
        secretAccessKey: secretAccessKey,
        region: 'Asia Pacific (Mumbai) ap-south-1'
      }
    );
    const params = {
      Bucket: 'chc-uploads',
      Key: file_name,
      Body: file,
      ACL: 'public-read',
      ContentType: contentType
    };

    return bucket.upload(params, function (err, data) {
      if (err) {
        console.log('There was an error uploading your file: ', err);
        localStorage.setItem('docDataStatus', 'false');
        return false;

      }
      else {
        console.log('Successfully uploaded file from service');
        return true;
      }
    });
  }
}

访问密钥和秘密访问密钥是在我的代码中静态输入的,所以不用担心。 由于安全问题,我只是将这些更改为变量名,同时将此问题发布到堆栈溢出。

任何帮助,将不胜感激。

尽管这不能回答您最初的问题,正如 Marcin 用您的 AWS 凭证所说的那样,将它们硬编码到您的代码中是非常糟糕的做法,应该不惜一切代价避免。 对于前端应用程序,这可以通过使用简单的 API 端点为您生成签名上传 URL 到 S3 存储桶来执行:

https://aws.amazon.com/blogs/developer/generate-presigned-url-modular-aws-sdk-javascript/

要真正回答您的问题,您可能会看到错误,因为您传递的区域格式不正确。

    const bucket = new S3(
      {
        region: 'ap-south-1'
      }
    );

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#region-property

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM