|
| 1 | +# React Native AWS S3 |
| 2 | + |
| 3 | +A React Native wrapper for AWS [iOS](https://github.com/aws/aws-sdk-ios)/[Android](https://github.com/aws/aws-sdk-android) S3 SDK. |
| 4 | + |
| 5 | +We currently implements `TransferUtility`, see [iOS](http://docs.aws.amazon.com/mobile/sdkforios/developerguide/s3transferutility.html)/[Android](http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/getting-started-store-retrieve-s3-transferutility.html) docs for more information. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +$ npm install react-native-s3 --save |
| 11 | +``` |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +#### iOS |
| 16 | + |
| 17 | +In XCode, in the project navigator: |
| 18 | + |
| 19 | +* Right click `Libraries` ➜ `Add Files to [your project's name]`, Add `node_modules/react-native-s3/ios/RNS3.xcodeproj`. |
| 20 | +* Add `libRNS3.a` to your project's `Build Phases` ➜ `Link Binary With Libraries` |
| 21 | +* Add `$(SRCROOT)/../node_modules/react-native-s3/ios/Frameworks` to your project's `Build Settings` ➜ `Framework Search Paths` |
| 22 | +* Add `node_modules/react-native-s3/ios/Frameworks/*.framework`, `libsqlite3.tbd`, `libz.tbd` to your project's `Build Phases` ➜ `Link Binary With Libraries` |
| 23 | +* Edit `AppDelegate.m` of your project |
| 24 | + |
| 25 | +```objective-c |
| 26 | +#import "RNS3TransferUtility.h" |
| 27 | + |
| 28 | +...... |
| 29 | + |
| 30 | +- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler { |
| 31 | + [RNS3TransferUtility interceptApplication:application |
| 32 | + handleEventsForBackgroundURLSession:identifier |
| 33 | + completionHandler:completionHandler] |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +* __*[Optional]*__ you can set the credentials in `AppDelegate.m` |
| 38 | + |
| 39 | +```objective-c |
| 40 | +#import "RNS3TransferUtility.h" |
| 41 | + |
| 42 | +...... |
| 43 | + |
| 44 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 45 | +{ |
| 46 | + [[RNS3TransferUtility nativeCredentialsOptions] setObject:@"eu-west-1" forKey:@"region"]; |
| 47 | + [[RNS3TransferUtility nativeCredentialsOptions] setObject:[NSNumber numberWithInt:[RNS3TransferUtility credentialType:@"BASIC"]] forKey:@"type"]; |
| 48 | + [[RNS3TransferUtility nativeCredentialsOptions] setObject:@"your_access_key_here" forKey:@"access_key"]; |
| 49 | + [[RNS3TransferUtility nativeCredentialsOptions] setObject:@"your_secret_key_here" forKey:@"secret_key"]; |
| 50 | + ...... |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +#### Android |
| 55 | + |
| 56 | +* Edit `android/settings.gradle` of your project: |
| 57 | + |
| 58 | +```gradle |
| 59 | +... |
| 60 | +include ':react-native-s3' |
| 61 | +project(':react-native-s3').projectDir = new File(settingsDir, '../node_modules/react-native-s3/android') |
| 62 | +``` |
| 63 | + |
| 64 | +* Edit `android/app/build.gradle` of your project: |
| 65 | + |
| 66 | +```gradle |
| 67 | +... |
| 68 | +dependencies { |
| 69 | + ... |
| 70 | + compile project(':react-native-s3') |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +* Add package to `MainActivity` |
| 75 | + |
| 76 | +```java |
| 77 | +...... |
| 78 | + |
| 79 | +import com.mybigday.rn.*; // import |
| 80 | + |
| 81 | +public class MainActivity extends ReactActivity { |
| 82 | + |
| 83 | + ...... |
| 84 | + |
| 85 | + @Override |
| 86 | + protected List<ReactPackage> getPackages() { |
| 87 | + return Arrays.<ReactPackage>asList( |
| 88 | + new MainReactPackage(), |
| 89 | + new RNS3Package() // add package |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +You can use [rnpm](https://github.com/rnpm/rnpm) instead of above steps. |
| 96 | + |
| 97 | +* Edit `android/app/src/main/AndroidManifest.xml` of your project: |
| 98 | + |
| 99 | +```xml |
| 100 | +<service |
| 101 | + android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" |
| 102 | + android:enabled="true" /> |
| 103 | +``` |
| 104 | + |
| 105 | +* __*[Optional]*__ you can set the credentials in `MainActivity`: |
| 106 | + |
| 107 | +```java |
| 108 | +@Override |
| 109 | +public void onCreate(Bundle savedInstanceState) { |
| 110 | + super.onCreate(savedInstanceState); |
| 111 | + |
| 112 | + RNS3TransferUtility.nativeCredentialsOptions.put("region", "eu-west-1"); |
| 113 | + RNS3TransferUtility.nativeCredentialsOptions.put("type", RNS3TransferUtility.CredentialType.BASIC); |
| 114 | + RNS3TransferUtility.nativeCredentialsOptions.put("access_key", "your_access_key_here"); |
| 115 | + RNS3TransferUtility.nativeCredentialsOptions.put("secret_key", "your_secret_key_here"); |
| 116 | +} |
| 117 | + |
| 118 | +``` |
| 119 | + |
| 120 | +## Usage |
| 121 | + |
| 122 | +```js |
| 123 | +import { transferUtility } from 'react-native-s3'; |
| 124 | +``` |
| 125 | + |
| 126 | +## API |
| 127 | + |
| 128 | +#### `transferUtility.setupWithNative()` |
| 129 | + |
| 130 | +Return: Boolean - `true` or `false` depending on the setup successful. |
| 131 | + |
| 132 | +#### `transferUtility.setupWithBasic(options)` |
| 133 | + |
| 134 | +* `options` Object |
| 135 | + * `region` String - a S3 Region (default: eu-west-1) |
| 136 | + * `access_key` String - the AWS access key ID |
| 137 | + * `secret_key` String - the AWS secret access key |
| 138 | + * `session_token` String - (optional) __(Android)__ |
| 139 | + |
| 140 | +Return: Promise - will resolve arguments: |
| 141 | +* Boolean - `true` or `false` depending on the setup successful. |
| 142 | + |
| 143 | +#### `transferUtility.setupWithCognito(options)` |
| 144 | + |
| 145 | +* `options` Object |
| 146 | + * `region` String - a S3 Region (default: eu-west-1) |
| 147 | + * `identity_pool_id` String - the Amazon Cogntio identity pool |
| 148 | + * `caching` Boolean - use `CognitoCachingCredentialsProvider` instead of `CognitoCredentialsProvider` __(Android)__ |
| 149 | + |
| 150 | +See AWS CognitoCredentialsProvider ([iOS](http://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSCognitoCredentialsProvider.html)/[Android](http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/com/amazonaws/auth/CognitoCredentialsProvider.html)) for more information. |
| 151 | + |
| 152 | +Return: Promise - will resolve arguments: |
| 153 | +* Boolean - `true` or `false` depending on the setup successful. |
| 154 | + |
| 155 | +#### `transferUtility.upload(options)` |
| 156 | + |
| 157 | +New a upload task. |
| 158 | + |
| 159 | +* `options` Object |
| 160 | + * `bucket` String - a S3 bucket name |
| 161 | + * `key` String - the object key in the bucket |
| 162 | + * `file` String - the file path to upload |
| 163 | + * `meta` Object |
| 164 | + * `contentType` String - the file content-type |
| 165 | + * `contentMD5` String - the file md5 hash |
| 166 | + |
| 167 | +Return: Promise - will resolve, see following arguments: |
| 168 | +* Object - a [Task](#the-task-object-structure) object |
| 169 | + |
| 170 | +or reject. |
| 171 | + |
| 172 | +#### `transferUtility.download(options)` |
| 173 | + |
| 174 | +New a download task. |
| 175 | + |
| 176 | +* `options` Object |
| 177 | + * `bucket` String - a S3 bucket name |
| 178 | + * `key` String - the object key in the bucket |
| 179 | + * `file` String - donwload save file path |
| 180 | + |
| 181 | +Return: Promise - will resolve, see following arguments: |
| 182 | +* Object - a [Task](#the-task-object-structure) object |
| 183 | + |
| 184 | +or reject. |
| 185 | + |
| 186 | +#### `transferUtility.pause(id)` |
| 187 | + |
| 188 | +* `id` Number - a Task id |
| 189 | + |
| 190 | +#### `transferUtility.resume(id)` |
| 191 | + |
| 192 | +* `id` Number - a Task id |
| 193 | + |
| 194 | +#### `transferUtility.cancel(id)` |
| 195 | + |
| 196 | +* `id` Number - a Task id |
| 197 | + |
| 198 | +#### `transferUtility.deleteRecord(id)` __(Android)__ |
| 199 | + |
| 200 | +* `id` Number - a Task id |
| 201 | + |
| 202 | +Return: Promise - will resolve, see following arguments: |
| 203 | +* Boolean - `true` or `false` depending on the delete task record successful. |
| 204 | + |
| 205 | +#### `transferUtility.getTask(id)` |
| 206 | + |
| 207 | +Gets a Task object with the given id. |
| 208 | + |
| 209 | +* `id` Number - a Task id |
| 210 | + |
| 211 | +Return: Promise - will resolve, see following arguments: |
| 212 | +* Object - a [Task](#the-task-object-structure) object |
| 213 | + |
| 214 | +#### `transferUtility.getTasks(type, idAsKey)` |
| 215 | + |
| 216 | +Gets a Task object list with the type. |
| 217 | + |
| 218 | +* `type` String - enum: `upload`, `download` |
| 219 | +* `idAsKey` Boolean - true: return Object with id as key, false: return Array |
| 220 | + |
| 221 | +Return: Promise - will resolve, see following arguments: |
| 222 | +* Array - a [Task](#the-task-object-structure) object list |
| 223 | + |
| 224 | +#### `transferUtility.subscribe(id, eventHandler)` |
| 225 | + |
| 226 | +Subscribe the task changes with the given id. |
| 227 | + |
| 228 | +* `id` Number - a Task id |
| 229 | +* `eventHandler` Function - arguments: |
| 230 | + * `task` Object - a [Task](#the-task-object-structure) object |
| 231 | + |
| 232 | +#### `transferUtility.unsubscribe(id)` |
| 233 | + |
| 234 | +Unsubscribe the task changes with the given id. |
| 235 | + |
| 236 | +* `id` Number - a Task id |
| 237 | + |
| 238 | +## The `Task` object structure |
| 239 | + |
| 240 | +```js |
| 241 | +{ |
| 242 | + id: Number, |
| 243 | + state: String, // task state |
| 244 | + // progress of task: bytes / totalBytes |
| 245 | + bytes: Number, |
| 246 | + totalBytes: Number, |
| 247 | + // iOS only part, waiting https://github.com/aws/aws-sdk-android/pull/105 |
| 248 | + bucket: String, |
| 249 | + key: String, |
| 250 | +} |
| 251 | +``` |
| 252 | + |
| 253 | +It will not be immediately refresh, you must `subscribe` or call `getTask(id)` to replace it. |
| 254 | + |
| 255 | +## The `Task` states |
| 256 | + |
| 257 | +* `waiting` |
| 258 | +* `in_progress` |
| 259 | +* `pause` |
| 260 | +* `canceled` |
| 261 | +* `completed` |
| 262 | +* `failed` |
| 263 | + |
| 264 | +## The `nativeCredentialsOptions` type |
| 265 | + |
| 266 | +* `BASIC` |
| 267 | +* `COGNITO` |
| 268 | + |
| 269 | +## Roadmap |
| 270 | + |
| 271 | +#### iOS |
| 272 | + |
| 273 | +- [x] TransferUtility |
| 274 | +- [ ] TransferManager |
| 275 | +- [ ] Bucket Control |
| 276 | +- CredentialsProvider |
| 277 | + - [ ] STS |
| 278 | + |
| 279 | +#### Android |
| 280 | + |
| 281 | +- [x] TransferUtility |
| 282 | +- [ ] TransferManager (Deprecated) |
| 283 | +- [ ] Bucket Control |
| 284 | +- CredentialsProvider |
| 285 | + - [ ] STS |
| 286 | + |
| 287 | +## License |
| 288 | + |
| 289 | +[MIT](LICENSE.md) |
0 commit comments