亚马逊s3文件下载返回404

时间:2018-07-03 09:01:44

标签: node.js amazon-web-services file amazon-s3 download

我正在尝试在s3中下载文件。我遵循了https://www.npmjs.com/package/s3上的s3库,但是无法下载文件。并且错误消息不清楚。它显示“错误:http状态代码404”。我想念什么?

var fs = require('fs');
var s3 = require('s3');
var stdio = require('stdio');

var client = s3.createClient({
  s3Options: {
    accessKeyId: "access key id",
    secretAccessKey: "secret access key",
  },
});



var params = {
    localFile: "home/download",

    s3Params: {
      Bucket:  "bucketname",
      Key: "/folder1/folder2/folder3/fileName",
    },
};

var downloader = client.downloadFile(params);
downloader.on('error', function(err) {
  console.error("unable to download:", err.stack);
  //err.stack returns as "Error: http status code 404"
});
downloader.on('progress', function() {
  console.log("progress", downloader.progressAmount, downloader.progressTotal);
});
downloader.on('end', function() {
  console.log("done downloading");
});

1 个答案:

答案 0 :(得分:0)

我调试了您的代码,并在S3上放置了一个私人文件,并在使用您提到的上述代码时遇到了相同的错误。 s3客户端初始化的问题是我修复了s3客户端。

如果您阅读文档,他们会提到如何使用AWS-S3进行配置 https://www.npmjs.com/package/s3#create-a-client-from-existing-awss3-object

var fs = require('fs');
var s3 = require('s3');
var stdio = require('stdio');
var AWS =require('aws-sdk')

var client1 = s3.createClient({
  maxAsyncS3: 20,     // this is the default
  s3RetryCount: 3,    // this is the default
  s3RetryDelay: 1000, // this is the default
  multipartUploadThreshold: 20971520, // this is the default (20 MB)
  multipartUploadSize: 15728640, // this is the default (15 MB)
  s3Options: {
    accessKeyId: "***********",
    secretAccessKey: "********"
  },
});




// initilize wtih s3 client
var awsS3Client = new AWS.S3(client1);
var options = {
  s3Client: awsS3Client,
  // more options available. See API docs below.
};
var client = s3.createClient(options);

var params = {
  // local directory with file name
    localFile: "./test.jpg",

    s3Params: {
      // bucket name 
      Bucket:  "cf-logs.test.com",
      // folder and file name to download
      Key: "test/Horse Racing.jpg",
    },
};

var downloader = client.downloadFile(params);
downloader.on('error', function(err) {
  console.error("unable to download:", err.stack);
  //err.stack returns as "Error: http status code 404"
});
downloader.on('progress', function() {
  console.log("progress", downloader.progressAmount, downloader.progressTotal);
});
downloader.on('end', function() {
  console.log("done downloading");
});

enter image description here