mhart/aws4fetch

S3 POST Method Not Allowed

bluecrystalsign opened this issue ยท 9 comments

Dear,

I',m trying to do 2 calls to the same object on S3 (GET and POST).
The user (of the API credentials) has AdministratorAccess and AmazonS3FullAccess . I'm getting a method not allowed answer for the POST.

I'm using the REST endpoint, not the WEBSITE endpoint.

MethodNotAllowedThe specified method is not allowed against this resource.POSTOBJECT3FBC4A28E4475E7EiLYyMMkmBD9iburQ8s+PptMqxagFQFjrclFoK458UNhjX7dCk4+qNNw+CrrDqi/i0Z5CSVr4Cw8=

Is there any limitation on the library? Have you tried something like this?

Thank you,
Sergio

mhart commented

This doesn't look like an error with this library (it's not a signing error).

Can you post the code you're using?

Thank you for your great work in this library, and your fast response.

My code is:

/**

  • Important constants
  • @param {string} bucket
  • @param {string} hostname
    */
    const bucket = ' REMOVED '
    const hostname = '.s3.amazonaws.com'

/**

  • Module Import
  • This module lets you access your AWS buckets
  • by wrapping your calls with AWS auth headers
  • @param {string} accessKeyId
  • @param {string} secretAccessKey
    */
    import { AwsClient } from 'aws4fetch'
    const aws = new AwsClient({
    accessKeyId: ' REMOVED ',
    secretAccessKey: ' REMOVED ',
    region: 'sa-east-1'
    })

/**

  • Redirects your call to an AWS bucket
  • @param {Request} request
    */

async function handleRequest(request) {
if (request.method === 'POST') {
console.log('handlePost')
const { method, body } = request
const url = new URL(request.url)

url.hostname = bucket + hostname

console.log(url.pathname)
console.log(url.hostname)
console.log(method)

const res = await aws.fetch(url, { body:  'bodyToPost'})    
const text = await res.text()

return new Response(text)

} else {
return new Response('NOT post')
}
}

addEventListener('fetch', event => {
let response = handleRequest(event.request);
event.respondWith(response)
})

It returns to me:

MethodNotAllowedThe specified method is not allowed against this resource.POSTOBJECT3DF3D1E9B2F09BBDTcwZj4R5ri42bAZqdPaNoXm5X6hUXqVGlSSkF/ftzkBdlLGIiboRWWh/o4RK72tskNrrKwW0OR0=

I have the same problem. I'd like to see an example of uploading something to S3. For the life of me, I just can't figure it out!

Did you try to use PUT ? (instead of POST)

I have the same problem, could you guys put a working example for s3 putObject, please?

mhart commented

The API Docs clearly state PutObject uses PUT, not POST โ€“ please try that:

https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html

mhart commented

Going to close this out โ€“ S3 uses PUT for uploading, not POST โ€“ that's the reason it's giving you the error

@ogkarademirciDH

const res = await aws.fetch(url, { body: 'bodyToPost', method: 'PUT})