Skip to content

Commit

Permalink
improve(wrangler): [R2] Output suggested wrangler.toml changes after …
Browse files Browse the repository at this point in the history
…creating a bucket (#7193)
  • Loading branch information
sdnts authored Nov 7, 2024
1 parent 1d5bc6d commit ad51d1d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-poets-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Output suggested wrangler.toml changes after creating an R2 bucket
48 changes: 36 additions & 12 deletions packages/wrangler/src/__tests__/r2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,15 @@ describe("r2", () => {
);
await runWrangler("r2 bucket create testBucket");
expect(std.out).toMatchInlineSnapshot(`
"Creating bucket 'testBucket'...
✅ Created bucket 'testBucket' with default storage class of Standard."
`);
"Creating bucket 'testBucket'...
✅ Created bucket 'testBucket' with default storage class of Standard.
Configure your Worker to write objects to this bucket:
[[r2_buckets]]
bucket_name = \\"testBucket\\"
binding = \\"testBucket\\""
`);
});

it("should create a bucket with the expected jurisdiction", async () => {
Expand All @@ -290,17 +296,29 @@ describe("r2", () => {
);
await runWrangler("r2 bucket create testBucket -J eu");
expect(std.out).toMatchInlineSnapshot(`
"Creating bucket 'testBucket (eu)'...
✅ Created bucket 'testBucket (eu)' with default storage class of Standard."
`);
"Creating bucket 'testBucket (eu)'...
✅ Created bucket 'testBucket (eu)' with default storage class of Standard.
Configure your Worker to write objects to this bucket:
[[r2_buckets]]
bucket_name = \\"testBucket\\"
binding = \\"testBucket\\""
`);
});

it("should create a bucket with the expected default storage class", async () => {
await runWrangler("r2 bucket create testBucket -s InfrequentAccess");
expect(std.out).toMatchInlineSnapshot(`
"Creating bucket 'testBucket'...
✅ Created bucket 'testBucket' with default storage class of InfrequentAccess."
`);
"Creating bucket 'testBucket'...
✅ Created bucket 'testBucket' with default storage class of InfrequentAccess.
Configure your Worker to write objects to this bucket:
[[r2_buckets]]
bucket_name = \\"testBucket\\"
binding = \\"testBucket\\""
`);
});

it("should error if storage class is invalid", async () => {
Expand Down Expand Up @@ -340,9 +358,15 @@ describe("r2", () => {
);
await runWrangler("r2 bucket create testBucket --location weur");
expect(std.out).toMatchInlineSnapshot(`
"Creating bucket 'testBucket'...
✅ Created bucket 'testBucket' with location hint weur and default storage class of Standard."
`);
"Creating bucket 'testBucket'...
✅ Created bucket 'testBucket' with location hint weur and default storage class of Standard.
Configure your Worker to write objects to this bucket:
[[r2_buckets]]
bucket_name = \\"testBucket\\"
binding = \\"testBucket\\""
`);
});
});

Expand Down
8 changes: 7 additions & 1 deletion packages/wrangler/src/r2/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { UserError } from "../errors";
import { logger } from "../logger";
import * as metrics from "../metrics";
import { requireAuth } from "../user";
import { getValidBindingName } from "../utils/getValidBindingName";
import { LOCATION_CHOICES } from "./constants";
import { createR2Bucket, isValidR2BucketName } from "./helpers";
import type {
Expand Down Expand Up @@ -68,8 +69,13 @@ export async function Handler(args: HandlerOptions) {
logger.log(
`✅ Created bucket '${fullBucketName}' with${
location ? ` location hint ${location} and` : ``
} default storage class of ${storageClass ? storageClass : `Standard`}.`
} default storage class of ${storageClass ? storageClass : `Standard`}.\n\n` +
"Configure your Worker to write objects to this bucket:\n\n" +
"[[r2_buckets]]\n" +
`bucket_name = "${args.name}"\n` +
`binding = "${getValidBindingName(args.name, "r2")}"`
);

await metrics.sendMetricsEvent("create r2 bucket", {
sendMetrics: config.send_metrics,
});
Expand Down

0 comments on commit ad51d1d

Please sign in to comment.