REST API upload to S3 access denied

I’m using the rest api to create the asset in ion, then I get the upload location access infos from the axios answer.
I use this credential then to upload to S3, but I get AccessDenied: Access Denied
at S3ServiceException.ServiceException [as constructor]

If I use my own bucket, it’s working, but that’s not what I want for now.
When I console log uploadLocation, I have an accessKey, a sessionToken etc.
Every thing is in order, that’s why I don’t understand why I get an AccessDenied.
What is missing ?

const S3ClientCred = {
      accessKeyId: uploadLocation.accessKey,
      secretAccessKey: uploadLocation.secretAccessKey,
      sessionToken: uploadLocation.sessionToken
    }
    const params = {
      Bucket: uploadLocation.bucket,
      Prefix: uploadLocation.prefix,
      Key: selectedFile.name,
      Body: selectedFile
    };
    try {
      const parallelUploads3 = new Upload({
        client: new S3Client({apiVersion: '2006-03-01', region: 'us-east-1',signatureVersion: 'v4',endpoint: uploadLocation.endpoint,  credentials: S3ClientCred}),
        params: params,
      });
      parallelUploads3.on("httpUploadProgress", (progress) => {
        console.log(progress);
      });
      await parallelUploads3.done();
      console.log('parallelUploads3.done()');
    } catch (e) {
      console.log(e);
    }

Thanks,

Hi - Have you taken a look at cesium-ion-rest-api-examples/index.js at main · CesiumGS/cesium-ion-rest-api-examples · GitHub?

Hi @Shehzan_Mohammed ,

Sure, I based my code on this example. I’m trying to do the same thing in react.

Hi

I don’t think your upload code as mentioned before matches the link I shared. For example, the s3.upload options are Body, Bucket, and Key, whereas in yours has a Prefix option too. The Bucket should just be the first part of the s3 path, and the Key should be the remaining path of the file(s) in the bucket.

See Class: AWS.S3 — AWS SDK for JavaScript for the S3 API.

Hi @Shehzan_Mohammed ,
Thank you !!! I had been staring at this code for so long, I thought I knew it by heart.
You made me look at the right place and my mistake was indeed with prefix.
Key: uploadLocation.prefix+selectedFile.name was the solution.
Thanks !!!