Implementation of an IntentService that receives a File and information about an S3 bucket. The service handles the upload of said file to the bucket.
Build an instance of S3BucketData and make a call to S3UploadService.upload():
S3Credentials s3Credentials = new S3Credentials(accessKey, secretKey, sessionToken);
S3BucketData s3BucketData = new S3BucketData.Builder()
.setCredentials(s3Credentials)
.setBucket(bucket)
.setKey(key)
.setRegion(region)
.build();
S3UploadService.upload(context, s3BucketData, file, null);
where key is:
key = pathToFile + "/" + fileName;
key could be just the fileName
keep in mind that the final URL will have the following format:
finalUrl = "https://" + bucket + ".s3.amazonaws.com/" + key;
S3Callback is an optional parameter in the upload() method. If not null, it should contain a String indicating what action will be broadcasted after upload completion. Optionally, you can add a Serializable to receive extra data
s3Callback = new S3Callback("com.example.android.S3_UPLOAD_COMPLETED", "Awesome message");
If s3Callback is provided to the upload() method, after the upload is completed, the following lines will be executed inside S3UploadService:
Intent intent = new Intent(s3Callback.getActionCallback());
intent.putExtra(EXTRA_SERIALIZABLE, s3Callback.getExtra());
sendBroadcast(intent);
So if we have a BroadcastReceiver capturing the specified action, it will receive the broadcast message along with the string "Awesome message"
Add the JitPack repository to your root build.gradle:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Add the Gradle dependency:
dependencies {
compile 'com.github.OneCodeLabs:S3UploadService:1.0.0@aar'
}
Declare S3UploadService in your manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="$package_name">
<application
...>
...
<service
android:name="com.onecode.s3.service.S3UploadService"
android:exported="false" />
</application>
</manifest>