IBM / ibm-cos-sdk-js

ibm-cos-sdk-js
Apache License 2.0
38 stars 20 forks source link

getSignedUrlPromise - upload results in 403 #97

Closed iksinski closed 1 year ago

iksinski commented 1 year ago

Hey all, I'm having problems with uploading files with presigned url, I'm using HMAC keys generated via cli and code to generate url as follows:

import { S3, Credentials } from "ibm-cos-sdk";

const cos = new S3({
  endpoint: deps.env.cosEndpoint,
  apiKeyId: deps.env.cosApiKey,
  signatureVersion: "v4",
  credentials: new Credentials(
    deps.env.cosAccessKey,
    deps.env.cosSecretAccessKey,
  ),
  region: "eu-de",
});

const signedUrlParams = {
  Bucket: deps.env.cosBucket,
  Key,
  Expires: 600,
};

const uploadUrl = await cos.getSignedUrlPromise(
  "putObject",
  signedUrlParams,
);

Uploading files is done by a regular html form using this url, unfortunately it results in 403 error, There were headers present in the request which were not signed and header mentioned is host. What am I doing wrong...?

arnabm28 commented 1 year ago

@iksinski

403 Error as it suggests is The HTTP 403 Forbidden response status code indicates that the server understands the request but refuses to authorize it.

So are you sure if the endpoint provided has complete access for the upload. Also in the above example if I can understand correctly the PUTOBJECT request is being called via the URL being constructed.

Can you try the above request using the PUT Object request as shown in the example.

function createTextFile(bucketName, itemName, fileText) {
    console.log(`Creating new item: ${itemName}`);
    return cos.putObject({
        Bucket: bucketName, 
        Key: itemName, 
        Body: fileText
    }).promise()
    .then(() => {
        console.log(`Item: ${itemName} created!`);
        logDone();
    })
    .catch(logError);
}

Thanks.

iksinski commented 1 year ago

@arnabm28 thanks for your response. We've been using PUSH instead of PUT in the frontend - that's why we couldn't upload files... Sorry for not checking thoroughly before posting! Btw, it would be great to have docs for this for nodejs as well :)

arnabm28 commented 1 year ago

Thanks for the confirmation. Here is the S3 API Documentation link : https://ibm.github.io/ibm-cos-sdk-js/AWS/S3.html

The overall API documentation : https://ibm.github.io/ibm-cos-sdk-js/