You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A valid alternative design to presigned URLs is to use a Worker with a [binding](/workers/runtime-apis/bindings/) that implements your security policy.
@@ -72,14 +62,33 @@ A binding is defined in the Wrangler file of your Worker project's directory.
72
62
73
63
:::
74
64
75
-
A possible use case may be restricting an application to only be able to upload to a specific URL. With presigned URLs, your central signing application might look like the following JavaScript code running on Cloudflare Workers, workerd, or another platform.
65
+
Refer to [Use R2 from Workers](/r2/api/workers/workers-api-usage/) to learn how to bind a bucket to a Worker and use the binding to interact with your bucket.
66
+
67
+
## Generate presigned URLs
68
+
69
+
Generate a presigned URL by referring to the following examples:
70
+
71
+
-[AWS SDK for Go](/r2/examples/aws/aws-sdk-go/#generate-presigned-urls)
72
+
-[AWS SDK for JS v3](/r2/examples/aws/aws-sdk-js-v3/#generate-presigned-urls)
73
+
-[AWS SDK for JS](/r2/examples/aws/aws-sdk-js/#generate-presigned-urls)
74
+
-[AWS SDK for PHP](/r2/examples/aws/aws-sdk-php/#generate-presigned-urls)
A possible use case may be restricting an application to only be able to upload to a specific URL. With presigned URLs, your central signing application might look like the following JavaScript code running on Cloudflare Workers, workerd, or another platform (you might have to update the code based on the platform you are using).
76
80
77
-
If the Worker received a request for `https://example.com/uploads/dog.png`, it would respond with a presigned URL allowing a user to upload to your R2 bucket at the `/uploads/dog.png` path.
81
+
If the application received a request for `https://example.com/uploads/dog.png`, it would respond with a presigned URL allowing a user to upload to your R2 bucket at the `/uploads/dog.png` path.
82
+
83
+
To create a presigned URL, you will need to either use a package that implements the signing algorithm or implement the signing algorithm yourself. In this example, the `aws4fetch` package is used. You also need to have an access key ID and secret access key. Refer to [R2 API tokens](/r2/api/tokens/) for more information.
78
84
79
85
```ts
80
86
import { AwsClient } from"aws4fetch";
81
87
82
-
const r2 =newAwsClient({
88
+
// Create a new client
89
+
// Replace with your own access key ID and secret access key
90
+
// Make sure to store these securely and not expose them
91
+
const client =newAwsClient({
83
92
accessKeyId: "",
84
93
secretAccessKey: "",
85
94
});
@@ -98,6 +107,7 @@ export default {
98
107
returnnewResponse("Missing a filepath", { status: 400 });
99
108
}
100
109
110
+
// Replace with your bucket name and account ID
101
111
const bucketName ="";
102
112
const accountId ="";
103
113
@@ -111,7 +121,7 @@ export default {
111
121
// Specify a custom expiry for the presigned URL, in seconds
112
122
url.searchParams.set("X-Amz-Expires", "3600");
113
123
114
-
const signed =awaitr2.sign(
124
+
const signed =awaitclient.sign(
115
125
newRequest(url, {
116
126
method: "PUT",
117
127
}),
@@ -128,12 +138,16 @@ export default {
128
138
} satisfiesExportedHandler;
129
139
```
130
140
131
-
Notice the total absence of any configuration or token secrets present in the Worker code. Instead, in your [Wrangler configuration file](/workers/wrangler/configuration/), you would create a [binding](/r2/api/workers/workers-api-usage/#3-bind-your-bucket-to-a-worker) to whatever bucket represents the bucket you will upload to. Additionally, authorization is handled in-line with the upload which can reduce latency.
141
+
## Differences between presigned URLs and R2 binding
142
+
143
+
When using the R2 binding, you will not need any token secrets in your Worker code. Instead, in your [Wrangler configuration file](/workers/wrangler/configuration/), you will create a [binding](/r2/api/workers/workers-api-usage/#3-bind-your-bucket-to-a-worker) to your R2 bucket. Additionally, authorization is handled in-line which can reduce latency.
132
144
133
-
In some cases, Workers lets you implement certain functionality more easily. For example, if you wanted to offer a write-once guarantee so that users can only upload to a path once, with pre-signed URLs, you would need to sign specific headers and require the sender to send them. You can modify the previous Worker to sign additional headers:
145
+
However, when using presigned URLs, you will need to create and use the token secrets in your Worker code.
146
+
147
+
In some cases, Workers lets you implement certain functionality more easily. For example, if you wanted to offer a write-once guarantee so that users can only upload to a path once, with pre-signed URLs, you would need to sign specific headers and require the sender to send the same headers. You can modify the previous example to sign additional headers:
134
148
135
149
```ts
136
-
const signed =awaitr2.sign(
150
+
const signed =awaitclient.sign(
137
151
newRequest(url, {
138
152
method: "PUT",
139
153
}),
@@ -146,34 +160,41 @@ const signed = await r2.sign(
146
160
);
147
161
```
148
162
149
-
Note that the caller has to add the same `If-Unmodified-Since` header to use the URL. The caller cannot omit the header or use a different header. If the caller uses a different header, the presigned URL signature would not match, and they would receive a `403/SignatureDoesNotMatch`.
Note that the caller has to add the same `If-Unmodified-Since` header to use the URL. The caller cannot omit the header or use a different header since the signature covers the headers. If the caller uses a different header, the presigned URL signature would not match, and they would receive a `403/SignatureDoesNotMatch`.
150
175
151
-
In a Worker, you would change your upload to:
176
+
If you are using R2 bindings, you would change your upload to:
// No objects will have been uploaded before September 28th, 2021 which
182
+
// is the initial R2 announcement.
183
+
uploadedBefore: newDate(1632844800000),
163
184
},
164
-
);
185
+
});
165
186
if (existingObject?.etag!==request.headers.get("etag")) {
166
187
returnnewResponse("attempt to overwrite object", { status: 400 });
167
188
}
168
189
```
169
190
170
191
Cloudflare Workers currently have some limitations that you may need to consider:
171
192
172
-
- You cannot upload more than 100 MiB (200 MiB for Business customers) to a Worker.
193
+
- You cannot upload more than 100 MiB (200 MiB for Business customers) with a Worker.
173
194
- Enterprise customers can upload 500 MiB by default and can ask their account team to raise this limit.
174
195
- Detecting [precondition failures](/r2/api/s3/extensions/#conditional-operations-in-putobject) is currently easier with presigned URLs as compared with R2 bindings.
175
196
176
-
Note that these limitations depends on R2's extension for conditional uploads. Amazon's S3 service does not offer such functionality at this time.
197
+
Note that these limitations depend on R2's extension for conditional uploads. Amazon's S3 service does not offer such functionality at this time.
177
198
178
199
## Differences between presigned URLs and public buckets
0 commit comments