Skip to content

Commit

Permalink
Add support for metadata in Storage.copyObject (dart-archive/gcloud#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasfj authored Sep 27, 2024
1 parent 535244d commit c061502
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkgs/gcloud/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.8.14
- Support override metadata properties in `copyObject`.

## 0.8.13
- Support the latest version `^13.0.0` of the `googleapis` package.

Expand Down
7 changes: 5 additions & 2 deletions pkgs/gcloud/lib/src/storage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ class _StorageImpl implements Storage {
}

@override
Future copyObject(String src, String dest) {
Future copyObject(String src, String dest, {ObjectMetadata? metadata}) {
var srcName = _AbsoluteName.parse(src);
var destName = _AbsoluteName.parse(dest);
metadata ??= _ObjectMetadata();
var objectMetadata = metadata as _ObjectMetadata;
final object = objectMetadata._object;
return _api.objects
.copy(storage_api.Object(), srcName.bucketName, srcName.objectName,
.copy(object, srcName.bucketName, srcName.objectName,
destName.bucketName, destName.objectName)
.then((_) => null);
}
Expand Down
4 changes: 3 additions & 1 deletion pkgs/gcloud/lib/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@ abstract class Storage {
/// Copy object [src] to object [dest].
///
/// The names of [src] and [dest] must be absolute.
Future copyObject(String src, String dest);
///
/// [metadata] can be used to overwrite metadata properties.
Future copyObject(String src, String dest, {ObjectMetadata? metadata});
}

/// Information on a specific object.
Expand Down
2 changes: 1 addition & 1 deletion pkgs/gcloud/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: gcloud
version: 0.8.13
version: 0.8.14
description: >-
High level idiomatic Dart API for Google Cloud Storage, Pub-Sub and Datastore.
repository: https://github.com/dart-lang/gcloud
Expand Down
46 changes: 46 additions & 0 deletions pkgs/gcloud/test/storage/e2e_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,52 @@ void main() {
testCreateReadDelete('test-2', bytesResumableUpload);
});

testWithBucket('create-copy-read-delete', (bucket) async {
final bytes = [1, 2, 3];
final info = await bucket.writeBytes('test-for-copy', bytes);
expect(info, isNotNull);

await storage.copyObject(
bucket.absoluteObjectName('test-for-copy'),
bucket.absoluteObjectName('test'),
);

final result =
await bucket.read('test').fold<List<int>>([], (p, e) => p..addAll(e));
expect(result, bytes);

await bucket.delete('test');
await bucket.delete('test-for-copy');
});

testWithBucket('create-copy-metadata-read-delete', (bucket) async {
final bytes = [1, 2, 3];
final info = await bucket.writeBytes(
'test-for-copy',
bytes,
metadata: ObjectMetadata(contentType: 'text/plain'),
);
expect(info, isNotNull);

await storage.copyObject(
bucket.absoluteObjectName('test-for-copy'),
bucket.absoluteObjectName('test'),
metadata: ObjectMetadata(contentType: 'application/octet'),
);

final r1 = await bucket.info('test-for-copy');
expect(r1.metadata.contentType, 'text/plain');
final r2 = await bucket.info('test');
expect(r2.metadata.contentType, 'application/octet');

final result =
await bucket.read('test').fold<List<int>>([], (p, e) => p..addAll(e));
expect(result, bytes);

await bucket.delete('test');
await bucket.delete('test-for-copy');
});

group('create-read-delete-streaming', () {
void testCreateReadDelete(String name, List<int> bytes) {
testWithBucket(name, (bucket) async {
Expand Down

0 comments on commit c061502

Please sign in to comment.