Skip to content

Commit f65d3c9

Browse files
authored
chore(docs): Use urlOrPathname when relevant (#859)
* chore(docs): Use urlOrPathname when relevant * changeset
1 parent f232754 commit f65d3c9

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

.changeset/huge-coats-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vercel/blob': patch
3+
---
4+
5+
copy, head and del can receive a blob url or pathname, until now it was not very clear.

packages/blob/src/copy.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export interface CopyBlobResult {
1616
* Copies a blob to another location in your store.
1717
* Detailed documentation can be found here: https://vercel.com/docs/vercel-blob/using-blob-sdk#copy-a-blob
1818
*
19-
* @param fromUrl - The blob URL to copy. You can only copy blobs that are in the store, that your 'BLOB_READ_WRITE_TOKEN' has access to.
19+
* @param fromUrlOrPathname - The blob URL (or pathname) to copy. You can only copy blobs that are in the store, that your 'BLOB_READ_WRITE_TOKEN' has access to.
2020
* @param toPathname - The pathname to copy the blob to. This includes the filename.
2121
* @param options - Additional options. The copy method will not preserve any metadata configuration (e.g.: 'cacheControlMaxAge') of the source blob. If you want to copy the metadata, you need to define it here again.
2222
*/
2323
export async function copy(
24-
fromUrl: string,
24+
fromUrlOrPathname: string,
2525
toPathname: string,
2626
options: CopyCommandOptions,
2727
): Promise<CopyBlobResult> {
@@ -67,7 +67,10 @@ export async function copy(
6767
headers['x-cache-control-max-age'] = options.cacheControlMaxAge.toString();
6868
}
6969

70-
const params = new URLSearchParams({ pathname: toPathname, fromUrl });
70+
const params = new URLSearchParams({
71+
pathname: toPathname,
72+
fromUrl: fromUrlOrPathname,
73+
});
7174

7275
const response = await requestApi<CopyBlobResult>(
7376
`?${params.toString()}`,

packages/blob/src/del.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import type { BlobCommandOptions } from './helpers';
55
* Deletes one or multiple blobs from your store.
66
* Detailed documentation can be found here: https://vercel.com/docs/vercel-blob/using-blob-sdk#delete-a-blob
77
*
8-
* @param url - Blob url or array of blob urls that identify the blobs to be deleted. You can only delete blobs that are located in a store, that your 'BLOB_READ_WRITE_TOKEN' has access to.
8+
* @param urlOrPathname - Blob url (or pathname) to delete. You can pass either a single value or an array of values. You can only delete blobs that are located in a store, that your 'BLOB_READ_WRITE_TOKEN' has access to.
99
* @param options - Additional options for the request.
1010
*/
1111
export async function del(
12-
url: string[] | string,
12+
urlOrPathname: string[] | string,
1313
options?: BlobCommandOptions,
1414
): Promise<void> {
1515
await requestApi(
1616
'/delete',
1717
{
1818
method: 'POST',
1919
headers: { 'content-type': 'application/json' },
20-
body: JSON.stringify({ urls: Array.isArray(url) ? url : [url] }),
20+
body: JSON.stringify({
21+
urls: Array.isArray(urlOrPathname) ? urlOrPathname : [urlOrPathname],
22+
}),
2123
signal: options?.abortSignal,
2224
},
2325
options,

packages/blob/src/head.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ interface HeadBlobApiResponse extends Omit<HeadBlobResult, 'uploadedAt'> {
5454
* Fetches metadata of a blob object.
5555
* Detailed documentation can be found here: https://vercel.com/docs/vercel-blob/using-blob-sdk#get-blob-metadata
5656
*
57-
* @param url - Blob url to lookup.
57+
* @param urlOrPathname - Blob url or pathname to lookup.
5858
* @param options - Additional options for the request.
5959
*/
6060
export async function head(
61-
url: string,
61+
urlOrPathname: string,
6262
options?: BlobCommandOptions,
6363
): Promise<HeadBlobResult> {
64-
const searchParams = new URLSearchParams({ url });
64+
const searchParams = new URLSearchParams({ url: urlOrPathname });
6565

6666
const response = await requestApi<HeadBlobApiResponse>(
6767
`?${searchParams.toString()}`,

test/next/src/app/vercel/blob/script.mts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ console.log('=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*');
1717
console.log();
1818

1919
async function run(): Promise<void> {
20+
let count = 0;
21+
let hasMore = true;
22+
let cursor: string | undefined;
23+
while (hasMore) {
24+
// eslint-disable-next-line no-await-in-loop -- [@vercel/style-guide@5 migration]
25+
const listResult = await vercelBlob.list({
26+
cursor,
27+
});
28+
if (listResult.blobs.length > 0) {
29+
await vercelBlob.del(listResult.blobs.map((blob) => blob.url));
30+
}
31+
hasMore = listResult.hasMore;
32+
cursor = listResult.cursor;
33+
count += listResult.blobs.length;
34+
}
35+
2036
const urls = await Promise.all([
2137
textFileExample(),
2238
textFileNoRandomSuffixExample(),
@@ -33,7 +49,7 @@ async function run(): Promise<void> {
3349
createFolder(),
3450
manualMultipartUpload(),
3551
manualMultipartUploader(),
36-
// cancelPut(),
52+
cancelPut(),
3753

3854
// The following stream examples will fail when targeting the local api-blob because the server doesn't buffer
3955
// the request body, so we have no idea of the size of the file we need to put in S3
@@ -61,19 +77,21 @@ async function run(): Promise<void> {
6177
);
6278

6379
// list all blobs
64-
let count = 0;
65-
let hasMore = true;
66-
let cursor: string | undefined;
80+
count = 0;
81+
hasMore = true;
6782
while (hasMore) {
6883
// eslint-disable-next-line no-await-in-loop -- [@vercel/style-guide@5 migration]
6984
const listResult = await vercelBlob.list({
7085
cursor,
7186
});
87+
console.log(listResult);
7288
hasMore = listResult.hasMore;
7389
cursor = listResult.cursor;
7490
count += listResult.blobs.length;
7591
}
7692

93+
console.log(filteredUrls, 'filtered urls');
94+
7795
console.log(count, 'blobs in this store');
7896

7997
await Promise.all(filteredUrls.map((url) => vercelBlob.del(url)));
@@ -92,10 +110,16 @@ async function textFileExample(): Promise<string> {
92110
},
93111
);
94112
const head = await vercelBlob.head(blob.url);
95-
console.log(head);
113+
console.log('URL head', head);
114+
console.log(
115+
'pathname head',
116+
await vercelBlob.head(
117+
`some/new-folder/file-with-chars%20!'()@@{}[]-#?file.txt`,
118+
),
119+
);
96120
const copy = await vercelBlob.copy(
97-
blob.url,
98-
`some/even-new-folder/file-with-chars%20!'()@@{}[]-#?file.txt`,
121+
`some/new-folder/file-with-chars%20!'()@@{}[]-#?file.txt`,
122+
`YO.txt`,
99123
{
100124
access: 'public',
101125
},

0 commit comments

Comments
 (0)