2

The relevant packages I'm using are aws-sdk, multer-s3, and multer.

I have the upload setup as such:

aws.config.update({
    secretAccessKey: "accessKey",
    accessKeyId: "keyId",
    region: "eu-west-2"
});

var s3 = new aws.S3();

const upload = multer({
    storage: multerS3({
        s3,
        acl: "public-read",
        bucket: "my-buckets-name",
        metadata: function(req, file, cb) {
            console.log("passed1"); // prints
            cb(null, {
                fieldName: "file.fieldname"
            });
        },
        key: function(req, file, cb) {
            console.log("passed2"); // prints
            cb(null, Date.now().toString());
        }
    })
});

My route goes something like this,

router.post(
        "/single",
        [auth, upload.single("image")],
        async (req, res) => {
                console.log("hereiam"); // never reached
                res.json({
                    msg: "Server received media. Processing..."
                });

                if (mediaType === "image") {
                    try { /*...*/ } catch { /*...*/ }
                } else { /*...*/ }

Both of the passed1 and passed2 are outputted to the console, and I'm positive that my access key and key ID are correct. When I try to upload an image, I'm thrown a 403 error.

I'm using react native, and I was doing

const aws = require("aws-sdk/dist/aws-sdk-react-native");

That gave me absolutely no feedback at all, the program just continued going as if it nothing happened after I clicked "upload."

Then I changed the nodejs import to,

const aws = require("aws-sdk");

And now I'm being thrown a 403. Not sure what I'm doing wrong.


Edit:

Permissions of my S3:

img

Community
  • 1
  • 1
Mike K
  • 7,621
  • 14
  • 60
  • 120
  • Are you sure you have the correct permissions for your keys? Can you list the permissions that you have added for the key? – Nicolas El Khoury Aug 28 '19 at 13:58
  • Sure, I updated the OP – Mike K Aug 28 '19 at 14:04
  • These are the permissions of the S3 bucket. I would like the permissions of the IAM user to which the access keys are located. You may find this in the IAM section in the AWS console – Nicolas El Khoury Aug 28 '19 at 14:06
  • I don't have an IAM user. Rather, I was faced with [this](https://i.imgur.com/QDv16Dy.png) prompt, and I just selected `Continue to security credentials` because this is a personal project, and I didn't want to bother with security and best practices just yet. Should I create an IAM user, which may fix this 403 issue / would it provide you a better means of providing me some insight of how to resolve this 403 issue if I did create an IAM user? – Mike K Aug 28 '19 at 14:12
  • [This is all I see](https://i.imgur.com/Od00Iyn.png) at this point, as far as the AWS console goes – Mike K Aug 28 '19 at 14:16
  • This explains why you may not be able to upload files. Navigate to IAM console, then go to users section, find the user that you are using, and to which your keys belong, and list all the permissions for this user. Also, just to be sure, you are replacing the following: secretAccessKey: "accessKey", accessKeyId: "keyId" with the correct values, right? – Nicolas El Khoury Aug 28 '19 at 14:21
  • [Here you go](https://i.imgur.com/qIKYgd5.png). And yes, yes I am replacing them :P – Mike K Aug 28 '19 at 14:25

1 Answers1

0

Can you try this solution instead?

const AWS = require('aws-sdk');
const s3Client = new AWS.S3();
let params = {
   Bucket: <the bucket>,
   Key: <the key>,
   Body: <The file>
};
s3Client.upload(params, function(err, data) {
   if (err) console.log(err);
   else 
       <do something>       
 });

This is how I usually use the API. Note that the AWS credentials are passed through environment variables, but you can still pass them the way you are. Logically speaking, administrator access should allow you to upload files to S3, provided you are adding the credentials correctly. However, I use the method I just posted and not what you use.

Nicolas El Khoury
  • 5,867
  • 4
  • 18
  • 28
  • Thanks for what you provided -- at the ` if (s3Err) console.log('err', err);` line, it keeps printing an object with an id and a date field, for example `err { _id: 5d66ab5df3232c73e02b803e, date: 2019-08-28T16:27:09.998Z }` – Mike K Aug 28 '19 at 16:29
  • It should be err not s3Err, I made a mistake – Nicolas El Khoury Aug 29 '19 at 08:51
  • I replaced it in the code, I copied it from your answer here. I currently have it as `if(err) console.log('err', err);` – Mike K Aug 29 '19 at 09:42
  • Yes, I'm guessing it's the id of the error, but I'm trying to figure out where the error is being logged – Mike K Aug 29 '19 at 11:55
  • Can you print and display the error you are receiving? that doesnt make any sense at all. Also what is the type and size of the file that you are uploading? – Nicolas El Khoury Aug 29 '19 at 12:57
  • And what is the version of the AWS SDK package that you are using – Nicolas El Khoury Aug 29 '19 at 12:57