Skip to content

feat: add cli arg and interactive prompt for bucket name - #244

Open
arsdehnel wants to merge 4 commits into
ascorbic:mainfrom
arsdehnel:allow-custom-bucket-name
Open

arsdehnel wants to merge 4 commits into
ascorbic:mainfrom
arsdehnel:allow-custom-bucket-name

Conversation

@arsdehnel

@arsdehnel arsdehnel commented Sep 9, 2026 •

Copy link
Copy Markdown

Closes #107

In contrast to #201 I added the bucket naming to the create-pds CLI's variables here. I think the auto-naming convention of wrangler is useful but for the reasons mentioned in that PR's discussion I think this use case might not be a good one for it.

Docs

I updated a few places in the docs that reference buckets. Most notably was a change only indirectly related to my code change. There were two places that mentioned that bucket names must be globally unique. I don't believe that's the case with Cloudflare's buckets (although it definitely is the case with other cloud provider bucket implementations).

create-pds

I added an optional CLI arg for --blobs-bucket-name which effectively defaults to pds-blobs although to allow for the "should we prompt" logic I didn't use citty's default option and instead followed the pattern of projectName. I think write that to the wrangler.jsonc after copying it over from the template directory following the package.json pattern.

Tests

I added a few tests but not sure how you will feel about the basic string-matching of the assertions. Open to other ideas.

Changeset

I've never managed or contributed to a monorepo that uses that so hopefully I got that right!

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience here! This is a good approach, I just have a few comments before it can be merged

Comment on lines +266 to +280
let bucketName = args["blobs-bucket-name"];
if (!bucketName) {
if (nonInteractive) {
bucketName = "pds-blobs";
} else {
const result = await p.text({
message: "Blobs bucket name (must be unique in your Cloudflare account):",
placeholder: "pds-blobs",
defaultValue: "pds-blobs",
});
if (p.isCancel(result)) {
p.cancel("Cancelled");
process.exit(0);
}
bucketName = result;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name needs validating before it goes into the config. Running the built CLI with --blobs-bucket-name 'My_Bucket"x' exits 0 and writes "bucket_name": "My_Bucket"x", which is invalid JSONC. Uppercase or underscores give valid JSON that then fails at deploy.

R2's rules are 3–63 characters, lowercase letters, digits and hyphens, with no leading or trailing hyphen. Could you add a validate to the p.text prompt and an error exit when the flag value fails the same check? A test for the rejected case would be good too.

let bucketName = args["blobs-bucket-name"];
if (!bucketName) {
if (nonInteractive) {
bucketName = "pds-blobs";

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With pds-blobs as the default, a second PDS created with --yes (or by pressing enter at the prompt) gets the same bucket as the first, and wrangler binds to the existing one without complaint. Blob keys are prefixed with the DID so nothing gets overwritten, but it isn't the per-instance default #107 is after.

How about defaulting to ${projectName}-blobs, here and in the prompt's placeholder/defaultValue? pds init can rename the worker afterwards so the two can still differ, but it's a much better starting point.

"blobs-bucket-name": {
type: "string",
description: "Cloudflare R2 bucket name for blobs",
required: false,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: required: false is the default in citty, so this can go.

p.cancel("Cancelled");
process.exit(0);
}
bucketName = result;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the folder-name prompt above does result || "my-pds". Clack's defaultValue covers the empty case, but matching that pattern keeps the two prompts consistent.

// Uploads are staged under `<did>/staged/` until a record references
// them. Add a lifecycle rule to expire abandoned uploads, e.g.:
// wrangler r2 bucket lifecycle add pds-blobs --prefix "<did>/staged/" --expire-days 7
// wrangler r2 bucket lifecycle add <bucket-name> --prefix "<did>/staged/" --expire-days 7

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaceInFile uses replaceAll, so you can use {{blobsBucketName}} here as well and the generated command stays copy-pasteable.

expect(wrangler).toContain('"my-custom-bucket"');
});

it("leaves no unreplaced placeholders in wrangler.jsonc", () => {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads the directory created by the "default bucket name" test, so it fails when run on its own. Could you move the not.toContain("{{") assertion into that test?


- Not authenticated. Run `pnpm wrangler login`.
- The R2 bucket name is taken (R2 bucket names are global). Edit `wrangler.jsonc` to use a different name.
- The R2 bucket name is taken (R2 bucket names must be unique within an account). Edit `wrangler.jsonc` to use a different name.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right that names are per-account, thanks for catching that. It does mean this cause no longer applies though: if the name already exists in your own account, wrangler binds to that bucket and the deploy succeeds. The failure people can actually hit is an invalid name, so I'd reword this to something like:

Suggested change
- The R2 bucket name is taken (R2 bucket names must be unique within an account). Edit `wrangler.jsonc` to use a different name.
- The R2 bucket name is invalid. Names must be 3–63 characters of lowercase letters, digits and hyphens, and cannot start or end with a hyphen. Edit `wrangler.jsonc` to fix it.

```

The `binding` is what the Worker uses (`env.BLOBS`). The `bucket_name` is the R2 bucket — bucket names are global across all of Cloudflare. Pick a unique name.
The `binding` is what the Worker uses (`env.BLOBS`). The `bucket_name` is the R2 bucket which must be unique to your account but (unlike some other blog storage services) do not need to be globally unique. The `create-pds` CLI prompts you for this and will write it to this file, otherwise just provide one here if you're managing the `wrangler.jsonc` manually.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo ("blog storage") and the sentence has drifted a bit. Suggested wording:

Suggested change
The `binding` is what the Worker uses (`env.BLOBS`). The `bucket_name` is the R2 bucket which must be unique to your account but (unlike some other blog storage services) do not need to be globally unique. The `create-pds` CLI prompts you for this and will write it to this file, otherwise just provide one here if you're managing the `wrangler.jsonc` manually.
The `binding` is what the Worker uses (`env.BLOBS`). The `bucket_name` is the R2 bucket. Bucket names are unique within your Cloudflare account, so two PDSes with the same `bucket_name` share one bucket. The `create-pds` CLI asks for the name and writes it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: allow customization of cloudflare r2 bucket name

2 participants