aws s3 獲取所有文件_如何从aws s3 bucket获取文件?

邴宏大
2023-12-01

我目前无法将aws sdk包放入servicenow。我需要下载一个文件/从s3存储桶中获取对象,但我的代码结果只提供存储桶中所有内容的列表。如果我把对象的确切地址作为端点,我会得到一些无法验证规范地址的行

我现在已经能够将aws4包和cryptojs包投入服务。

var creds= { accessKeyId: 'access key' ,

secretAccessKey: 'secretAccessKey'

};

var opts = {

host: "mybucket.s3.amazonaws.com",

path: '/',

region: 'us-east-2',

myService:'s3',

myMethod: 'GET'

};

function getAmzDate(dateStr) {

var chars = [":","-"];

for (var i=0;i

while (dateStr.indexOf(chars[i]) != -1) {

dateStr = dateStr.replace(chars[i],"");

}

}

dateStr = dateStr.split(".")[0] + "Z";

return dateStr;

}

function getSignatureKey(Crypto, key, dateStamp, regionName, serviceName) {

var kDate = Crypto.HmacSHA256(dateStamp, "AWS4" + key);

var kRegion = Crypto.HmacSHA256(regionName, kDate);

var kService = Crypto.HmacSHA256(serviceName, kRegion);

var kSigning = Crypto.HmacSHA256("aws4_request", kService);

return kSigning;

}

var amzDate = getAmzDate(new Date().toISOString());

var authDate = amzDate.split("T")[0];

gs.log(amzDate);

gs.log(authDate);

var payload = '';

var hashedPayload = x_aws_sigv4.CryptoJS.SHA256(payload).toString();

gs.log(hashedPayload);

var canonicalReq = opts.myMethod + '\n' +

opts.path + '\n' +

'\n' +

'host:' + opts.host + '\n' +

'x-amz-content-sha256:' + hashedPayload + '\n' +

'x-amz-date:' + amzDate + '\n' +

'\n' +

'host;x-amz-content-sha256;x-amz-date' + '\n' +

hashedPayload;

var canonicalReqHash

=x_aws_sigv4.CryptoJS.SHA256(canonicalReq).toString();

var stringTosign = 'AWS4-HMAC-SHA256\n' +amzDate + '\n' +authDate+'/'+

opts.region+'/'+ opts.myService+'/aws4_request\n'+canonicalReqHash;

var signingKey = getSignatureKey(x_aws_sigv4.CryptoJS,

creds.secretAccessKey, authDate, opts.region, opts.myService);

var authKey = x_aws_sigv4.CryptoJS.HmacSHA256(stringTosign, signingKey);

var authString = 'AWS4-HMAC-SHA256 ' +

'Credential='+

creds.accessKeyId+'/'+

authDate+'/'+

opts.region+'/'+

opts.myService+'/aws4_request,'+

'SignedHeaders=host;x-amz-content-sha256;x-amz-date,'+

'Signature='+authKey;

var options = {

headers: { 'Accept': 'application/json' },

};

gs.log(authString);

var rm = new sn_ws.RESTMessageV2();

for (var h in options.headers) {

rm.setRequestHeader(h, options.headers[h]);

}

rm.setHttpMethod('GET');

rm.setEndpoint('https://' + opts.host);

rm.setRequestHeader('Authorization', authString);

rm.setRequestHeader('Host', opts.host);

rm.setRequestHeader('X-Amz-Date', amzDate );

rm.setRequestHeader('x-amz-content-sha256', hashedPayload );

var response = rm.execute();

var body = response.getBody();

gs.log(body.toString());

我应该让实际的文件自动导入到服务中

 类似资料: