繁体   English   中英

从S3 getObject()检索图像不起作用

[英]Retrieving image from S3 getObject() not working

我正在尝试从S3获取图像,即使我认为我给了正确的信息,我似乎也无法获得图像。

目前,它在几秒钟后返回一个空值,这使页面看起来好像已损坏/服务器5秒钟没有响应。

路线

// GET TEST
    app.get('/test',
    isAuthenticated,
    (req, res) => {
        var image;
        var params = {
            Bucket: "<bucket name>",
            Key: "<tried both the Etag and the file name both failed>"  
        };
        s3.getObject(params, (err, data) => {
           image = data;
           console.log('Data: ',data);
           res.render('test/test', {header: 'TEST', image: image});
       });
       console.log('Image: ', image);
    });

通过阅读文档,我知道getObject()似乎是我所需要的,但事实证明,使其很难工作。

AWS配置

var aws = require("aws-sdk"),
    s3 = new aws.S3();
var config = require('../config/config');
aws.config.update({
   secretAccessKey: config.aws_s3.secretAccessKey,
   accessKeyId: config.aws_s3.accessKeyId,
   region: config.aws_s3.region
});

这是从我的代码库的已知工作部分中摘录的。

我缺少文件未下载的信息吗? 如果文件是直接发送到客户端而不是我的服务器的,那么我也很喜欢。

编辑

我已经得到它给我一些数据,但仍然不会显示图像

数据

Data:  { AcceptRanges: 'bytes',
  LastModified: 2018-09-22T01:53:39.000Z,
  ContentLength: 2117,
  ETag: '"<Etag>"',
  ContentType: 'application/octet-stream',
  ServerSideEncryption: 'AES256',
  Metadata: {},
  Body: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 30 00 00 00 30 08 06 00 00 00 57 02 f9 87 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 00 04 ... > }

我终于解决了这个问题,与我一直在尝试的和链接的相比,它采取了一种截然不同的方法来获取它。 比链接方法简单得多。

起作用的方法是getSignedUrl ,它使用了我之前尝试使用的getOjbect函数。

我的路线

// GET TEST
    app.get('/test',
    isAuthenticated,
    (req, res) => {
        var urlParams = {Bucket: config.aws_s3.logoBucket, Key: <key>'};
        logoBucket.getSignedUrl('getObject', urlParams, function(err, url){
            res.render('test/test', {header: 'TEST', url: url});
        });
    });

配置

var aws = require("aws-sdk");
var config = require('../config/config');
aws.config.update({
   secretAccessKey: config.aws_s3.secretAccessKey,
   accessKeyId: config.aws_s3.accessKeyId,
   region: config.aws_s3.region,
  sslEnabled: true
});
var s3 = new aws.S3();
var logoBucket = new aws.S3( { params: {Bucket: config.aws_s3.logoBucket} } )

尝试这个:

首先,使用凭证配置配置文件

创建凭证文件或进行编辑(如果存在)
在Mac / Linux上为〜/ .aws / credentials或
Windows上的C:\\ Users \\ .aws \\ credentials

[/*Give Name to your profile*/]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key

与此更改代码

var AWS = require('aws-sdk');
AWS.config.region = '<Your region>';
var credentials = new AWS.SharedIniFileCredentials({profile: '<Your profile>'});
AWS.config.credentials = credentials;
var s3 = new aws.S3();

var params = {
  Bucket: "<Your bucket name>", 
  Key: "<file name to retrieve>"
 };


app.get('/test', isAuthenticated, function(req, res) {
  s3.getObject(params, function(err, data) {
    var image = data;
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
    res.render('test/test', {header: 'TEST', image: image});
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM