admin管理员组

文章数量:814911

我如何将凭据通知ManagedUpload对象?

我正在尝试使用aws-sdk中的ManagedUpload。我的代码:

var buffer = fs.readFileSync(filePath);

var paramsCreate = {
  Bucket: bucketName,
  Key: 'myfile.ogv',
  Body: buffer
};

let perc = 0;
var upload = new AWS.S3.ManagedUpload({
  partSize: 5 * 1024 * 1024, queueSize: 4,
  params: paramsCreate
}).on('httpUploadProgress',function(progress) {
  let newPerc = Math.round(progress.loaded / progress.total * 100);
  if (newPerc != perc) {
     perc = newPerc;
     console.log(perc + '% sent');
  }
});

let up = upload.promise();

up.then(data=>{
    console.log('Finished');
    console.log(data);
}).catch(err=>console.log('Err: ', err));

使用此代码,我(很明显)出现以下错误:

Error: connect EHOSTUNREACH 169.254.169.254:80
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
  errno: -113,
  code: 'CredentialsError',
  syscall: 'connect',
  address: '169.254.169.254',
  port: 80,
  time: 2020-05-08T23:29:33.528Z,
  originalError: {
    message: 'Could not load credentials from any providers',
    errno: -113,
    code: 'CredentialsError',
    syscall: 'connect',
    address: '169.254.169.254',
    port: 80,
    time: 2020-05-08T23:29:33.527Z,
    originalError: {
      message: 'EC2 Metadata roleName request returned error',
      errno: -113,
      code: 'EHOSTUNREACH',
      syscall: 'connect',
      address: '169.254.169.254',
      port: 80,
      time: 2020-05-08T23:29:33.526Z,
      originalError: [Object]
    }
  }
}

我说显然是因为我没有在任何地方通知凭据。但是,在ManagedUpload文档页面(here)中甚至没有提及。

如何将凭据通知aws lib以便与ManagedUpload一起使用?

编辑:

对于我的常规(非多部分)传输和常规S3访问,我通过这种方式通知凭据:

import dotenv from 'dotenv';

import AWS, { AWSError } from 'aws-sdk';
import {
  CreateBucketOutput,
  PutObjectOutput,
  GetBucketCorsOutput,
  GetObjectOutput,
  DeleteObjectOutput
} from 'aws-sdk/clients/s3';


dotenv.config();

let s3Options = {
    accessKeyId: process.env.CEPH_ACCESS_KEY,
    secretAccessKey: process.env.CEPH_SECRET_KEY,
    endpoint: process.env.CEPH_ENDPOINT,
    s3BucketEndpoint: false,
    s3ForcePathStyle: true
};

function s3Instance() {
  return new AWS.S3(s3Options);
}

这是此实例的使用示例:

export function sendObject(
  nameBucket,
  nameObject,
  data,
  metadata
) {
  metadata = metadata || {};
  const params = {
    Bucket: nameBucket,
    Key: nameObject,
    Body: data,
    Metadata: metadata
  };

  let perc = 0;
  return new Promise((resolve, reject) => {
    s3Instance()
    .putObject(params, (err, res) => {
      if (err) reject(err); else resolve(res);
    }).on('httpUploadProgress',function(progress) {
      let newPerc = Math.round(progress.loaded / progress.total * 100);
      if (newPerc != perc) {
         perc = newPerc;
         console.log(perc + '% sent');
      }
    });
  });
}
回答如下:

我真的很想念什么。 ManagedUpload具有服务参数选项。您可以将S3实例传递给它。所以我只是将更新功能更改为:

var upload = new AWS.S3.ManagedUpload({
  partSize: 5 * 1024 * 1024, queueSize: 4,
  service: s3Instance,
  params: paramsCreate
}).on('httpUploadProgress',function(progress) {
  let newPerc = Math.round(progress.loaded / progress.total * 100);
  if (newPerc != perc) {
     perc = newPerc;
     console.log(perc + '% sent');
  }
});

并且有效。

本文标签: 我如何将凭据通知ManagedUpload对象