如何将凭据通知给 ManagedUpload 对象?
How do I inform credentials to ManagedUpload object?
我正在尝试使用 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');
}
});
成功了。
我正在尝试使用 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');
}
});
成功了。