Skip to content

Commit e76bf2f

Browse files
committed
feat(storage_client): support copy/move to different bucket
1 parent 2e44d79 commit e76bf2f

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

packages/storage_client/lib/src/storage_file_api.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,21 @@ class StorageFileApi {
276276
/// example `folder/image.png`.
277277
/// [toPath] is the new file path, including the new file name. For example
278278
/// `folder/image-new.png`.
279-
Future<String> move(String fromPath, String toPath) async {
279+
///
280+
/// When copying to a different bucket, you have to specify the [destinationBucket].
281+
Future<String> move(
282+
String fromPath,
283+
String toPath, {
284+
String? destinationBucket,
285+
}) async {
280286
final options = FetchOptions(headers: headers);
281287
final response = await _storageFetch.post(
282288
'$url/object/move',
283289
{
284290
'bucketId': bucketId,
285291
'sourceKey': fromPath,
286292
'destinationKey': toPath,
293+
if (destinationBucket != null) 'destinationBucket': destinationBucket,
287294
},
288295
options: options,
289296
);
@@ -297,14 +304,21 @@ class StorageFileApi {
297304
///
298305
/// [toPath] is the new file path, including the new file name. For example
299306
/// `folder/image-copy.png`.
300-
Future<String> copy(String fromPath, String toPath) async {
307+
///
308+
/// When copying to a different bucket, you have to specify the [destinationBucket].
309+
Future<String> copy(
310+
String fromPath,
311+
String toPath, {
312+
String? destinationBucket,
313+
}) async {
301314
final options = FetchOptions(headers: headers);
302315
final response = await _storageFetch.post(
303316
'$url/object/copy',
304317
{
305318
'bucketId': bucketId,
306319
'sourceKey': fromPath,
307320
'destinationKey': toPath,
321+
if (destinationBucket != null) 'destinationBucket': destinationBucket,
308322
},
309323
options: options,
310324
);

packages/storage_client/test/client_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,5 +388,51 @@ void main() {
388388

389389
await storage.from(newBucketName).copy(uploadPath, "$uploadPath 2");
390390
});
391+
392+
test('copy to different bucket', () async {
393+
final storage = SupabaseStorageClient(
394+
storageUrl, {'Authorization': 'Bearer $storageKey'});
395+
396+
try {
397+
await storage.from('bucket2').download(uploadPath);
398+
fail('File that does not exist was found');
399+
} on StorageException catch (error) {
400+
expect(error.error, 'not_found');
401+
}
402+
await storage
403+
.from(newBucketName)
404+
.copy(uploadPath, uploadPath, destinationBucket: 'bucket2');
405+
try {
406+
await storage.from('bucket2').download(uploadPath);
407+
} catch (error) {
408+
fail('File that was copied was not found');
409+
}
410+
});
411+
412+
test('move to different bucket', () async {
413+
final storage = SupabaseStorageClient(
414+
storageUrl, {'Authorization': 'Bearer $storageKey'});
415+
416+
try {
417+
await storage.from('bucket2').download('$uploadPath 3');
418+
fail('File that does not exist was found');
419+
} on StorageException catch (error) {
420+
expect(error.error, 'not_found');
421+
}
422+
await storage
423+
.from(newBucketName)
424+
.move(uploadPath, '$uploadPath 3', destinationBucket: 'bucket2');
425+
try {
426+
await storage.from('bucket2').download('$uploadPath 3');
427+
} catch (error) {
428+
fail('File that was moved was not found');
429+
}
430+
try {
431+
await storage.from(newBucketName).download(uploadPath);
432+
fail('File that was moved was found');
433+
} on StorageException catch (error) {
434+
expect(error.error, 'not_found');
435+
}
436+
});
391437
});
392438
}

0 commit comments

Comments
 (0)