1
votes

I'm using Javascript AWS SDK. I include it with this:

All I want to do is GET objects already stored in an s3 bucket.

The client code has access to a (working) URL for the object stored on s3. It looks like this:

https://s3.amazonaws.com/my-bucket-name/my-object-key

My client app currently has a bunch of these URLs. I download the files successfully by simulating clicks on links of these URLs.

What I'd like to do is get all the files together in my script so that I can bundle them into a zip file with nice folder structure using JSzip.

I'm trying to do the following (this includes all s3 related code in my codebase):

    var bucketName = 'my-bucket-name';
    var s3 = new AWS.S3( {
        params: {
            Bucket: bucketName
        }
    } );

    var key = myFileUrl.split('my-bucket-name/')[1];
    var params = {
      Key: key
    };
    s3.getObject(params, function(err, data) {
      if (err) console.log("error!" + err +  err.stack); // an error occurred
      else {
        console.log("success getting file from S3:",data);           // successful response
      }
    });

When I run this, I get a "CredentialsError: Missing credentials in config" error.

I shouldn't actually need any credentials for this, right? The file is public, I can get it just by following the link. I looked at the AWS documentation for using the Javascript (client) sdk, and they don't set up a config object of any kind.

Has anyone gotten this to work? What am I doing wrong?

1
my script tag was deleted. I'm including the SDK in my stl with this file: sdk.amazonaws.com/js/aws-sdk-2.100.0.min.js - Jay

1 Answers

1
votes

The Javascript AWS SDK wraps the S3 REST API, which requires authentication on GetObject (see the note). If you don't want to provide authentication (note you could create a AMI role with only GetObject permission), you can just make a normal web request, such as with fetch or XMLHttpRequest if using a browser.