I'm not able to call AWS Api's using aws4fetch.
Could someone giveme an example on how to call S3 putobject using cloudflare workers ?
The below code worked for me to upload a simple txt file from a cloudflare worker to an amazon S3 bucket
import aws4fetch
const aws4fetch = require('aws4fetch')
declare your access key and secret that you saved when you created in aws the iam profile with permissions at least putobject and programmatic access (secure it by passing as a resource only the arn bucket you re interested)
const access_key = '<access_key_id_here>';
const access_secret = '<access_secret_here>';
const region = '<region>';
const bucket = '<bucket_name>';
Initialize aws4fetch client
const aws = new aws4fetch.AwsClient({
accessKeyId:access_key,
secretAccessKey:access_secret
});
const endpoint = 'https://'+bucket+'.s3.'+region+'.amazonaws.com/';
addEventListener('fetch', function(event) {
event.respondWith(handleRequest(event.request))
});
async function handleRequest(request) {
const filename_key = 'test.txt';
const content = `the content of the file`;
const res = await aws.fetch(endpoint+filename_key,
{ body: content, method: 'PUT'})
return new Response('ok')
}
Please note that you can also use aws-sdk too, which makes it more easier to interact with aws s3 but it will cost you at least 350kbytes of extra code (there is a limitation of 1mbyte worker script)