Dery Galeas
CDCloudflare Developers
•Created by Dery Galeas on 4/18/2025 in #general-help
Unauthorized Error when trying to "PUT" an image to R2 using Pre-Signed-Url.
Hello community, I am struggling with pre-signed-urls, for some reason after generating either PUT or GET urls and testing them in postman it returns an Unauthorized Error (401).
First I generated the API Token for my bucket, with the following configuration:
Permission: Object Read & Write
Specify Bucket: Apply to specific buckets only (select my specific bucket)
TTL: Forever
Client IP Address Filtering: As default
My code:
import {
GetObjectCommand,
PutObjectCommand,
S3Client,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { createTRPCRouter, publicProcedure } from "../context";
export const R2BucketRouter = createTRPCRouter({
getUploadURL: publicProcedure
.input(z.object({ filename: z.string() }))
.query(async ({ ctx, input }) => {
const S3 = new S3Client({
endpoint: "https://<account-id>.r2.cloudflarestorage.com/<bucket-name>",
credentials: {
accessKeyId: "",
secretAccessKey: "",
},
region: "auto",
});
const command = new PutObjectCommand({
Bucket: "store-bucket",
Key: input.filename,
ContentType: "image/png",
});
const signedUrl = await getSignedUrl(S3, command, {
expiresIn: 9999,
});
return { signedUrl };
})
After the url is generated I go to post-man, create a new PUT
request, paste the pre-signed-url, in the body section select the binary option the upload an image with the exact filename as the url was specified. Modified the Content-Type header to match image/png
Error:
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>Unauthorized</Code>
<Message>Unauthorized</Message>
</Error>
1 replies