Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3061,6 +3061,25 @@ class File extends ServiceObject<File> {
);
}

/**
* The public URL of this File
* Use {@link File#makePublic} to enable anonymous access via the returned URL.
*
* @returns {string}
*
* @example
* const {Storage} = require('@google-cloud/storage');
* const storage = new Storage();
* const bucket = storage.bucket('albums');
* const file = bucket.file('my-file');
*
* file.publicUrl();
Comment thread
shaffeeullah marked this conversation as resolved.
Outdated
* // Will return "https://storage.googleapis.com/albums/my-file"
*/
publicUrl(): string {
return `${STORAGE_POST_POLICY_BASE_URL}/${this.bucket.name}/${this.name}`;
Comment thread
shaffeeullah marked this conversation as resolved.
Outdated
}

move(
destination: string | Bucket | File,
options?: MoveOptions
Expand Down
42 changes: 42 additions & 0 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3455,6 +3455,48 @@ describe('File', () => {
});
});

describe('publicUrl', () => {
it('should return the public URL', done => {
const NAME = 'file-name';
const file = new File(BUCKET, NAME);
assert.strictEqual(
file.publicUrl(),
`https://storage.googleapis.com/bucket-name/${NAME}`
);
done();
});

it('with slash in the name', done => {
const NAME = 'parent/child';
const file = new File(BUCKET, NAME);
assert.strictEqual(
file.publicUrl(),
`https://storage.googleapis.com/bucket-name/${NAME}`
);
done();
});

it('with tilde in the name', done => {
const NAME = 'foo~bar';
const file = new File(BUCKET, NAME);
assert.strictEqual(
file.publicUrl(),
`https://storage.googleapis.com/bucket-name/${NAME}`
);
done();
});

it('with non ascii in the name', done => {
const NAME = '\u2603';
const file = new File(BUCKET, NAME);
assert.strictEqual(
file.publicUrl(),
`https://storage.googleapis.com/bucket-name/${NAME}`
);
done();
});
});

describe('isPublic', () => {
const sandbox = sinon.createSandbox();

Expand Down