/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npm run deploy` to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/
export default {
async fetch(request, env, ctx) {
if (request.method === 'OPTIONS') {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
"Access-Control-Max-Age": "86400",
}
});
}
if (request.method == 'GET') {
return new Response('Hello');
}
if (request.method === 'PUT') {
// Note that you could require authentication for all requests
// by moving this code to the top of the fetch function.
const auth = request.headers.get('Authorization');
const expectedAuth = `Bearer ${env.AUTH_SECRET}`;
if (!auth || auth !== expectedAuth) {
return new Response('Unauthorized', { status: 401 });
}
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
"Access-Control-Max-Age": "86400",
};
const url = new URL(request.url);
const key = url.pathname.slice(1);
await env.MY_BUCKET.put(key, request.body);
const data = `Object ${key} uploaded successfully!`;
// return new Response(`Object ${key} uploaded successfully!`);
return new Response(JSON.stringify(data), {
headers: {
...corsHeaders,
'Content-Type': 'application/json;charset=UTF-8'
}
})
}
},
};
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npm run deploy` to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/
export default {
async fetch(request, env, ctx) {
if (request.method === 'OPTIONS') {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
"Access-Control-Max-Age": "86400",
}
});
}
if (request.method == 'GET') {
return new Response('Hello');
}
if (request.method === 'PUT') {
// Note that you could require authentication for all requests
// by moving this code to the top of the fetch function.
const auth = request.headers.get('Authorization');
const expectedAuth = `Bearer ${env.AUTH_SECRET}`;
if (!auth || auth !== expectedAuth) {
return new Response('Unauthorized', { status: 401 });
}
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
"Access-Control-Max-Age": "86400",
};
const url = new URL(request.url);
const key = url.pathname.slice(1);
await env.MY_BUCKET.put(key, request.body);
const data = `Object ${key} uploaded successfully!`;
// return new Response(`Object ${key} uploaded successfully!`);
return new Response(JSON.stringify(data), {
headers: {
...corsHeaders,
'Content-Type': 'application/json;charset=UTF-8'
}
})
}
},
};