使用node.js服务器上传到amazon s3只能使用一次应该怎么解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (165)

我用来上传图片到服务器的代码只能使用一次

  1. 不知道我是否应该设置端点,s3教程中的所有示例都没有提到端点
  2. 不确定是否应该为每个要上传的文件继续制作新的s3对象
  3. 如果我没有设置端点,那么即使在第一次尝试时,上传也会失败(并提供与下面第二次上传相同的控制台输出)

上传功能一次工作,所以我认为这意味着我正确地设置了凭据。但让它再次运行的唯一方法是重新启动node.js应用程序。在第二次上传时,s3对象想要连接到本地主机,而不是我在创建时为其明确设置的端点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/' }
提问于
用户回答回答于
Please try Skipper:

Installation
$ npm install skipper-better-s3 --save


    const options =
          { // This is the usual stuff
            adapter: require('skipper-better-s3')
          , key: 'somekeyhere'
          , secret: 'dontsharethis'
          , bucket: 'my-s3-bucket'
          , region: 'us-east-1'  // Optional - default is 'us-standard'
            // Let's use the custom s3params to upload this file as publicly
            // readable by anyone
          , s3params:
            { ACL: 'public-read'
            }
            // And while we are at it, let's monitor the progress of this upload
          , onProgress: progress => sails.log.verbose('Upload progress:', progress)
          }

    req.file('avatar').upload(options, (err, files) => {
      // ... Continue as usual
    })

扫码关注云+社区

领取腾讯云代金券