R2: Create a presigned URL with Temporary access credentials
Hi! I'd like to know what's the proper way to create a presigned URL for an object with temporary access credentials. Is that even possible?
This is my try with typescript:
13 Replies
I'm passing the accessKeyId, secretAccessKey and sessionToken. But it seems that the signature generated when containing
X-Amz-Security-Token
does not really work, since cloudlflare returns this when accessing the URL:
When I do with non-Temporary access credentials, without passing the session token, then it works. So I'm starting to wonder if the temporary credentials have no permissions to create presignedURLs?
Btw, the temporary access credentials have read permissions to the resource. And I'm just trying to do a GET of the resource.
And more info, when I use normal credentials (not temporary access credentials), if I add also the token, when using the presigned URL I see:
Which is expected I guess. But makes me think that the temporary access credentials have no permissions for this operation.This is the endpoint I used to generate the credentials btw: https://developers.cloudflare.com/api/operations/r2-create-temp-access-credentials
Cloudflare API Documentation
Interact with Cloudflare's products and services via the Cloudflare API
bump, still couldn't find a solution..
bump
Hey, did you find what you were looking for, or do you still need help?
Hi! I couldn't manage to do it. My guess is that you can't presign URLs with a temporary token (or you can but the server won't accept it). This is possible in AWS S3 btw
Hmm. Let me look into it and get back to you
No news about this?
I got stuck with something else. Looking into this now.
Alright, I have tested this and it works. I am using the
@aws-sdk/s3-request-presigner
lib to generate the Pre-Signed URL, and the @aws-sdk/client-s3
to execute the S3 compatible commands.
While reading about the temporary access credentials, I learned that I need to pass the Secret Key as well. However, when I generate the Temporary Access Credentials, I noticed that it also generated a new Secret Key. You will have to use this new Secret Key with the generated Temporary Access Token for a successful request. I hope this helpsCould you share the code? It does not work for me. The R2 server does not accept a X-Amz-Security-Token parameter. Hence, it seems to not follow the S3 specification.
I of course, use the new Secret Key, Secret Key Id and the token
Did you generatw the temporary credentials with the R2 endpoint? Are they for a specific bucket or for global access?
Yes, I created the credential using
r2/temp-access-credentials
endpoint. Here's the body I send with the request:
I have the exact same. The parent key has full control of R2, but R2 keeps replying that X-Amz-Security-Token is not allowed. Or just gives a CORS error (but I found out google chrome shows all errors as CORS errors 😅 ). I'd be great to see an example of how you are using this.
Could it be the object I'm trying to access is "inside a folder" (e.g. whatever/whatever2/object.png). Even though I have tried in the "root" of the bucket and had the same problem I believe.
Could I have the code you use to generate the presigned URL?
I am using this
@aws-sdk/s3-request-presigner
lib to generate the presigned URL. In your code, are you passing the new Secret Key or using the one you generated earlier? This was something that I found a bit trickyI'm using all the data that R2 sends back from the r2/temp-access-credentials api call. So I'm using all the new secret key, id and token.
I'm gonna try with the package later and see if I make any difference. But the same exact function I used for R2 worked fine in S3.
Really late (I have been working on other stuff), but I confirm that with this package it works!
I don't remember which package I used before or how I used it, so I can't really compare. And there must be something wrong in my custom function when adding the temporary token info, but In any case, I can work from this now!