choose random image
I would like to have a bunch of files in R2 bucket, and somehow choose one of those to be main file sometimes. But I have completely no idea how to do it.
I saw a question about cron, which made me wonder, as I don't know how to even access server crontab so I can edit it. Or maybe this is just wrong way to do it. I am very far from expert.
7 Replies
thanks 🙂 yeah I am not remotely sure how I would do what I want to do.
that's fine. I just want to update the download url file with random file out of bunch with timer
Cloudflare R2+Workers! Set up your own cloud image hosting right away! - 二叉树树
Cloudflare R2+Workers! Set up your own cloud image hosting right aw...
This article is a remake of: https://xfeed.app/notes/71448-15 Result Image
Principle
The image source is hosted by Cloudflare R2, which…
hmm interesting, though it seems to require windows program for setup
That windows programm only uploads images to r2. You can use rclone on linux/macos, or use the web browser, as:
Since transferring files via the web interface is slow and does not support files larger than 300MB, we will use a locally deployed AList to connect to your R2 bucket for high-speed uploads.and if you are fine with slow or max 300mb, web interface would work
yeah. my biggest issue is somehow updating one image from set of images randomly with timer. I have completely no idea to do it completely via cloudflare
You can use a worker for that.
Create an array of images that you want to switch between.
If you are fine with one image after another:
Switching via timer can be done by taking a timestamp, dividing it through your interval, and calculating the modulos against your array length.
This way your worker will always return a specific image, calculated via time.
Though this will result in a request everytime the image is fetched.
If you really need a random image: worker + cron trigger, and copy (download+upload?) a file to your desired path. This will result in a request every time the image is swapped out.
my current system right now, my computer has cron to upload random file each 15 min. I would like to do it completely off my pc so I can be offline and it will still update. I plan to try that tutorial later when I am better 😄 Thanks for summary.
(its very hot where I am currently lol)
I tried to make worker using this site example but bucket.list errors out as invalid. I dont know. lol
export default {
async fetch(request, env, ctx) {
// R2 bucket configuration
const bucket = env.MY_BUCKET;
try {
// List all objects in the /ri/h directory
const objects = await bucket.list({ prefix: '' });
// Randomly select an object from the list
const items = objects.objects;
if (items.length === 0) {
return new Response('No images found', { status: 404 });
}
const randomItem = items[Math.floor(Math.random() * items.length)];
// Get the selected object
const object = await bucket.get(randomItem.key);
if (!object) {
return new Response('Image not found', { status: 404 });
}
// Set appropriate Content-Type
const headers = new Headers();
headers.set('Content-Type', object.httpMetadata.contentType || 'image/jpeg');
// Return image content
return new Response(object.body, { headers });
} catch (error) {
console.error('Error:', error);
return new Response('Internal Server Error', { status: 500 });
}
},
};
the error is from const objects = await bucket.list({ prefix: '' }); list is not a function. I dont know what would be proper there.
tried chatai for lols but yeah it keeps using list which dont seem to work
yeah I still cant figure it