JS AWS S3 SDK getSignedURL 不返回任何内容

JS AWS S3 SDK getSignedURL returning nothing

我是 AWS SDK 的新手,我正在尝试检索 S3 中私有对象的预签名 URL。

我发现我的承诺正在以 undefined 的值解析。我怀疑我的 awsBucket 变量没有利用我的 awsConfig 但我不确定如何让两者进行通信。

我的目标是单击一个按钮来更新 imgsrc 以显示存储在 S3 中的图像。非常感谢任何帮助。

下面是我的代码,您可以假设 secret/access 键已正确填充。

HTML:

<html>

  <head>
    <script type="text/javascript" src="lib/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="lib/bootstrap/dist/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/tableau.extensions.1.latest.js"></script>
    <script type="text/javascript" src="js/index.js"></script>

    <!-- AWS SDK-->
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.910.0.min.js"></script>
    
    <link rel="stylesheet" type="text/css" href="lib/bootstrap/dist/css/bootstrap.min.css">

    <!-- Font awesome icons-->
    <script src="https://kit.fontawesome.com/d8651426bf.js" crossorigin="anonymous"></script>

    <title>Image from S3</title>

  </head>

  <body>

    <div class="" id="divMain">
      <p>
        <button id="refreshImageButton" class="btn btn-secondary">
            <i class="fa fa-repeat"></i>
        </button>
      </p>
      <img
        id="displayImage"
        src=""
        alt="Placeholder"
      >
    </div>

  </body>

</html>

JS:


var awsAccessKeyID = 'my-access-key';
var awsSecretAccessKey = 'my-secret-key';
var awsBucketName = 'bucket-name';
const URL_EXPIRATION_TIME = 60; // in seconds

var awsRegion = 'eu-west-2'; // Is this the right way to write the AWS Region?

$(document).ready(initialise());

// This function is called when the page first loads
function initialise() {

    console.log('Test')
    // Refresh image button
    $("#refreshImageButton").click(function() {

      let imagePath='1.jpg'
      refreshImage(imagePath);
    });
  }, function () { console.log('Error while Initializing: ' + err.toString()); });
}

// This function refreshes the image using an incoming path from the data
function refreshImage(imagePath){
  console.log(`Image path: ${imagePath}`)

  let preSignedURL = generatePreSignedGetUrl(imagePath, 'jpg')

  console.log(preSignedURL)
  $("#displayImage").attr("src",preSignedURL);

}

//  This function generated the pre signed URL that can access a private object in S3
function generatePreSignedGetUrl( fileName , fileType) {
  return new Promise(function(resolve, reject){
    let awsConfig = new AWS.Config();
    awsConfig.update({
      accessKeyId: awsAccessKeyID,
      secretAccessKey: awsSecretAccessKey,
    })

    let awsBucket = new AWS.S3();
    let awsBucket = new AWS.S3({
      params: { Bucket: awsBucketName},
      region: awsRegion,
    })

    awsBucket.getSignedUrl('getObject', {
      Key: fileName,
      ContentType: fileType,
      Expires: URL_EXPIRATION_TIME
    } , (err , url) => {
      resolve(url) // API Response Here
    });
  })
};

最后,我放弃了这种方法,转而使用 NodeJS 后端。这是我完整的工作代码:


// Use modules
const config = require('./config');

const AWS = require('aws-sdk');
AWS.config.update({
  accessKeyId: config.awsAccessKeyID
  , secretAccessKey: config.awsSecretAccessKey
  , region: config.awsRegion
});

const s3 = new AWS.S3({signatureVersion: 'v4'});

// Create the parameters for calling listObjects
var bucketParams = {
  Bucket : 'BUCKETNAME',
};

// Call S3 to obtain a list of the objects in the bucket
// This is a good way to test the authentication is working
s3.listObjects(bucketParams, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});


var awsGeneratePresignedURLsBackend = function(app) {

  // Enable CORS
  app.use(function (req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
      next();
  });

  // Connection Test
  app.get('/ConnectionTest', function (req, res) {
      res.send('Connected');
  });

  // Slack App Connection Test
  app.post('/generatePreSignedGetUrl', function (req, res) {
      console.log("")
      console.log("--------------------------")
      console.log(`endpoint: generatePreSignedGetUrl`)
      console.log(`type: POST`)
      console.log(`statusCode: ${res.statusCode}`)

      const myBucket = req.body.awsBucketName
      const myKey = req.body.fileName

      console.log(`Bucket: ${myBucket}`);
      console.log(`Key: ${myKey}`);

      const url = s3.getSignedUrl('getObject', {
        Bucket: myBucket,
        Key: myKey
      });

      res.send(url)
      console.log(`url: ${url}`)

      res.end()
  });
}

module.exports = awsGeneratePresignedURLsBackend;