Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
halifox committed Dec 29, 2024
0 parents commit ed3c263
Show file tree
Hide file tree
Showing 28 changed files with 7,300 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/s3_storage.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## 1.0.2

- Intl Dependency updated

## 1.0.1

- Dependency updated

## 1.0.0

- Dependency updated
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 xuty

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
178 changes: 178 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<p align="center">
<h1 align="center">S3Storage Dart</h1>
</p>

This is the _unofficial_ S3 (AWS, MinIO) Dart Client SDK that provides simple APIs to access any Amazon S3 compatible object storage server.

<p align="center">
<a href="https://github.com/MindMayhem/s3_storage/actions/workflows/dart.yml">
<img src="https://github.com/MindMayhem/s3_storage/workflows/Dart/badge.svg">
</a>
<a href="https://pub.dev/packages/s3_storage">
<img src="https://img.shields.io/pub/v/s3_storage">
</a>
</p>


## API

| Bucket operations | Object operations | Presigned operations | Bucket Policy & Notification operations |
| ----------------------- | ------------------------ | --------------------- | --------------------------------------- |
| [makeBucket] | [getObject] | [presignedUrl] | [getBucketNotification] |
| [listBuckets] | [getPartialObject] | [presignedGetObject] | [setBucketNotification] |
| [bucketExists] | [fGetObject] | [presignedPutObject] | [removeAllBucketNotification] |
| [removeBucket] | [putObject] | [presignedPostPolicy] | [listenBucketNotification] |
| [listObjects] | [fPutObject] | | [getBucketPolicy] |
| [listObjectsV2] | [copyObject] | | [setBucketPolicy] |
| [listIncompleteUploads] | [statObject] | | |
| [listAllObjects] | [removeObject] | | |
| [listAllObjectsV2] | [removeObjects] | | |
| | [removeIncompleteUpload] | | |


## Usage

### Initialize MinIO Client

**MinIO**

```dart
final s3_storage = S3Storage(
endPoint: 'play.min.io',
accessKey: 'Q3AM3UQ867SPQQA43P2F',
secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
);
```

**AWS S3**

```dart
final s3_storage = S3Storage(
endPoint: 's3.amazonaws.com',
accessKey: 'YOUR-ACCESSKEYID',
secretKey: 'YOUR-SECRETACCESSKEY',
);
```

**Filebase**

```dart
final s3_storage = S3Storage(
endPoint: 's3.filebase.com',
accessKey: 'YOUR-ACCESSKEYID',
secretKey: 'YOUR-SECRETACCESSKEY',
useSSL: true,
);
```

**File upload**
```dart
import 'package:s3_storage/io.dart';
import 'package:s3_storage/s3_storage.dart';
void main() async {
final s3_storage = S3Storage(
endPoint: 'play.min.io',
accessKey: 'Q3AM3UQ867SPQQA43P2F',
secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
);
await s3_storage.fPutObject('mybucket', 'myobject', 'path/to/file');
}
```

For complete example, see: [example]

> To use `fPutObject()` and `fGetObject`, you have to `import 'package:s3_storage/io.dart';`
**Upload with progress**
```dart
import 'package:s3_storage/s3_storage.dart';
void main() async {
final s3_storage = S3Storage(
endPoint: 'play.min.io',
accessKey: 'Q3AM3UQ867SPQQA43P2F',
secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
);
await s3_storage.putObject(
'mybucket',
'myobject',
Stream<Uint8List>.value(Uint8List(1024)),
onProgress: (bytes) => print('$bytes uploaded'),
);
}
```

**Get object**

```dart
import 'dart:io';
import 'package:s3_storage/s3_storage.dart';
void main() async {
final s3_storage = S3Storage(
endPoint: 's3.amazonaws.com',
accessKey: 'YOUR-ACCESSKEYID',
secretKey: 'YOUR-SECRETACCESSKEY',
);
final stream = await s3_storage.getObject('BUCKET-NAME', 'OBJECT-NAME');
// Get object length
print(stream.contentLength);
// Write object data stream to file
await stream.pipe(File('output.txt').openWrite());
}
```

## Features and bugs

Please file feature requests and bugs at the [issue tracker][tracker].

Contributions to this repository are welcome.

## License

MIT

[tracker]: https://github.com/MindMayhem/s3_storage/issues
[example]: https://pub.dev/packages/s3_storage#-example-tab-
[link text itself]: http://www.reddit.com

[makeBucket]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/makeBucket.html
[listBuckets]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/listBuckets.html
[bucketExists]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/bucketExists.html
[removeBucket]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/removeBucket.html
[listObjects]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/listObjects.html
[listObjectsV2]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/listObjectsV2.html
[listIncompleteUploads]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/listIncompleteUploads.html
[listAllObjects]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/listAllObjects.html
[listAllObjectsV2]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/listAllObjectsV2.html

[getObject]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/getObject.html
[getPartialObject]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/getPartialObject.html
[putObject]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/putObject.html
[copyObject]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/copyObject.html
[statObject]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/statObject.html
[removeObject]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/removeObject.html
[removeObjects]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/removeObjects.html
[removeIncompleteUpload]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/removeIncompleteUpload.html

[fGetObject]: https://pub.dev/documentation/s3_storage/latest/io/StorageX/fGetObject.html
[fPutObject]: https://pub.dev/documentation/s3_storage/latest/io/StorageX/fPutObject.html

[presignedUrl]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/presignedUrl.html
[presignedGetObject]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/presignedGetObject.html
[presignedPutObject]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/presignedPutObject.html
[presignedPostPolicy]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/presignedPostPolicy.html

[getBucketNotification]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/getBucketNotification.html
[setBucketNotification]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/setBucketNotification.html
[removeAllBucketNotification]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/removeAllBucketNotification.html
[listenBucketNotification]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/listenBucketNotification.html

[getBucketPolicy]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/getBucketPolicy.html
[setBucketPolicy]: https://pub.dev/documentation/s3_storage/latest/s3_storage/S3Storage/setBucketPolicy.html
14 changes: 14 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Defines a default set of lint rules enforced for
# projects at Google. For details and rationale,
# see https://github.com/dart-lang/pedantic#enabled-lints.
include: package:pedantic/analysis_options.yaml

# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
# Uncomment to specify additional rules.
# linter:
# rules:
# - camel_case_types

analyzer:
# exclude:
# - path/to/excluded/files/**
Binary file added example/custed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions example/minio_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'dart:io';

import 'package:s3_storage/s3_storage.dart';

void main() async {
final s3storage = S3Storage(
endPoint: 'play.min.io',
signingType: SigningType.V2,
accessKey: 'Q3AM3UQ867SPQQA43P2F',
secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
);

print(await s3storage.putObject('aaaaaaaaa', 'test.png',
File('example/custed.png').readAsBytes().asStream(),
metadata: {'test': 'works'}));
}
83 changes: 83 additions & 0 deletions lib/io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:path/path.dart' show dirname;
import 'package:s3_storage/src/s3.dart';
import 'package:s3_storage/src/s3_errors.dart';
import 'package:s3_storage/src/s3_helpers.dart';

extension StorageX on S3Storage {
// Uploads the object using contents from a file
Future<String> fPutObject(
String bucket,
String object,
String filePath, [
Map<String, String>? metadata,
]) async {
StorageInvalidBucketNameError.check(bucket);
StorageInvalidObjectNameError.check(object);

metadata ??= {};
metadata = insertContentType(metadata, filePath);
metadata = prependXAMZMeta(metadata);

final file = File(filePath);
final stat = await file.stat();
if (stat.size > maxObjectSize) {
throw StorageError(
'$filePath size : ${stat.size}, max allowed size : 5TB',
);
}

return putObject(
bucket,
object,
file.openRead().cast<Uint8List>(),
size: stat.size,
metadata: metadata,
);
}

/// Downloads and saves the object as a file in the local filesystem.
Future<void> fGetObject(
String bucket,
String object,
String filePath,
) async {
StorageInvalidBucketNameError.check(bucket);
StorageInvalidObjectNameError.check(object);

final stat = await statObject(bucket, object);
final dir = dirname(filePath);
await Directory(dir).create(recursive: true);

final partFileName = '$filePath.${stat.etag}.part.s3storage';
final partFile = File(partFileName);
IOSink partFileStream;
var offset = 0;

final rename = () {
partFile.rename(filePath);
};

if (await partFile.exists()) {
final localStat = await partFile.stat();
if (stat.size == localStat.size) return rename();
offset = localStat.size;
partFileStream = partFile.openWrite(mode: FileMode.append);
} else {
partFileStream = partFile.openWrite(mode: FileMode.write);
}

final dataStream = await getPartialObject(bucket, object, offset);
await dataStream.pipe(partFileStream);

final localStat = await partFile.stat();
if (localStat.size != stat.size) {
throw StorageError(
'Size mismatch between downloaded file and the object');
}

return rename();
}
}
2 changes: 2 additions & 0 deletions lib/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'src/s3_models.dart';
export 'src/s3_models_generated.dart';
5 changes: 5 additions & 0 deletions lib/s3_storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library s3storage;

export 'src/s3.dart';
export 'src/s3_errors.dart';
export 'src/s3_stream.dart';
Loading

0 comments on commit ed3c263

Please sign in to comment.