使用node.js服务器上传到amazon s3只能运行一次

时间:2018-06-10 22:33:02

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

所以这是我用来将图像上传到服务器的代码,但它只能运行一次

[在接受的答案的评论中回答]

  1. 我不知道我是否应该设置端点,s3教程中的所有示例都没有提到端点

  2. 我不确定我是否应该为每个要上传的文件制作新的s3对象

  3. 如果我没有设置端点,即使第一次尝试上传也总是失败(并提供与下面发布的第二次上传相同的控制台输出)

  4. 上传功能只运行一次,所以我认为这意味着我正确设置了凭据。 但让它再次运行的唯一方法是重新启动node.js应用程序。 我很确定我错过了很多东西。 特别是我注意到在第二次上传时,s3对象想要连接到localhost,而不是我在制作时为其明确设置的端点url。

    function upload_photo(socket, data) {
    	s3 = new AWS.S3( {endpoint: 'http://s3-ap-southeast-1.amazonaws.com'} );
    	
    	console.log("s3 endpoint", s3.endpoint); // for testing
    
    	var myBucket = 'blackjack-profilepics';	
    	var myKey = socket.player.user_id +".txt";
    
    	params = {Bucket: 'blackjack-profilepics', Key: myKey, Body: data };
    	s3.putObject(params, function(err, data) {
    	if (err) {
    		console.log("Error uploading data:",err)
    		console.log("s3 endpoint", s3.endpoint);
    	} else {
    		console.log("Successfully uploaded data to myBucket/%s", myKey);
    	}
      });

    这是第一次和后续上传尝试的控制台输出:

    first upload:
    0|blackjac | s3 endpoint Endpoint {
    0|blackjac |   protocol: 'http:',
    0|blackjac |   host: 's3-ap-southeast-1.amazonaws.com',
    0|blackjac |   port: 80,
    0|blackjac |   hostname: 's3-ap-southeast-1.amazonaws.com',
    0|blackjac |   pathname: '/',
    0|blackjac |   path: '/',
    0|blackjac |   href: 'http://s3-ap-southeast-1.amazonaws.com/' }
    0|blackjac | done with upload!
    0|blackjac | Successfully uploaded data to myBucket/15.txt
    
    second upload:
    0|blackjac | s3 endpoint Endpoint {
    0|blackjac |   protocol: 'http:',
    0|blackjac |   host: 's3-ap-southeast-1.amazonaws.com',
    0|blackjac |   port: 80,
    0|blackjac |   hostname: 's3-ap-southeast-1.amazonaws.com',
    0|blackjac |   pathname: '/',
    0|blackjac |   path: '/',
    0|blackjac |   href: 'http://s3-ap-southeast-1.amazonaws.com/' }
    0|blackjac | done with upload!
    0|blackjac | Error uploading data: { UnknownEndpoint: Inaccessible host: `blackjack-profilepics.localhost'. This service may not be available in the `ap-southeast-1' region.
    0|blackjac |     at Request.ENOTFOUND_ERROR (/home/ubuntu/node_modules/aws-sdk/lib/event_listeners.js:456:46)
    0|blackjac |     at Request.callListeners (/home/ubuntu/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    0|blackjac |     at Request.emit (/home/ubuntu/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    0|blackjac |     at Request.emit (/home/ubuntu/node_modules/aws-sdk/lib/request.js:683:14)
    0|blackjac |     at ClientRequest.error (/home/ubuntu/node_modules/aws-sdk/lib/event_listeners.js:295:22)
    0|blackjac |     at ClientRequest.<anonymous> (/home/ubuntu/node_modules/aws-sdk/lib/http/node.js:89:19)
    0|blackjac |     at emitOne (events.js:96:13)
    0|blackjac |     at ClientRequest.emit (events.js:188:7)
    0|blackjac |     at Socket.socketErrorListener (_http_client.js:310:9)
    0|blackjac |     at emitOne (events.js:96:13)
    0|blackjac |   message: 'Inaccessible host: `blackjack-profilepics.localhost\'. This service may not be available in the `ap-southeast-1\' region.',
    0|blackjac |   code: 'UnknownEndpoint',
    0|blackjac |   region: 'ap-southeast-1',
    0|blackjac |   hostname: 'blackjack-profilepics.localhost',
    0|blackjac |   retryable: true,
    0|blackjac |   originalError:
    0|blackjac |    { Error: getaddrinfo ENOTFOUND blackjack-profilepics.localhost blackjack-profilepics.localhost:8000
    0|blackjac |        at errnoException (dns.js:28:10)
    0|blackjac |        at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
    0|blackjac |      message: 'getaddrinfo ENOTFOUND blackjack-profilepics.localhost blackjack-profilepics.localhost:8000',
    0|blackjac |      code: 'NetworkingError',
    0|blackjac |      errno: 'ENOTFOUND',
    0|blackjac |      syscall: 'getaddrinfo',
    0|blackjac |      hostname: 'blackjack-profilepics.localhost',
    0|blackjac |      host: 'blackjack-profilepics.localhost',
    0|blackjac |      port: 8000,
    0|blackjac |      region: 'ap-southeast-1',
    0|blackjac |      retryable: true,
    0|blackjac |      time: 2018-06-10T22:15:58.926Z },
    0|blackjac |   time: 2018-06-10T22:15:58.926Z }
    0|blackjac | s3 endpoint Endpoint {
    0|blackjac |   protocol: 'http:',
    0|blackjac |   host: 's3-ap-southeast-1.amazonaws.com',
    0|blackjac |   port: 80,
    0|blackjac |   hostname: 's3-ap-southeast-1.amazonaws.com',
    0|blackjac |   pathname: '/',
    0|blackjac |   path: '/',
    0|blackjac |   href: 'http://s3-ap-southeast-1.amazonaws.com/' }

2 个答案:

答案 0 :(得分:1)

在第二次上传期间,它似乎正在尝试在localhost域上查找您的存储桶。您可以尝试指定区域而不是端点吗?

S3服务对象应该以任何方式将该信息提供给端点。

s3 = new AWS.S3({region: 'ap-southeast-1'})

答案 1 :(得分:1)

我遇到了类似的问题,此配置对我有用。

const bucketname = 'bsbucket';
//configuring the aws environment
aws.config.update({
    region:"us-west-1",
    accessKeyId:"<accessKeyId>",
    secretAccessKey:"<secretAccessKey>",
    s3BucketEndpoint:true,
    endpoint:"http://" + bucketname + ".s3.amazonaws.com"
});