Reputation: 706
I added the AWS Android SDK to my app and i have some code in an AsyncTask that should upload my video to my bucket but its not working.
Android code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
try {
Uri selectedImageUri = data.getData();
File file = new File(String.valueOf(selectedImageUri));
// Initialize the Amazon Cognito credentials provider
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"us-east-1:5bfc6b1a-6193-4fXc-823f-ba21a9fadc1b", // Identity Pool ID
Regions.US_EAST_1 // Region
);
// Create an S3 client
AmazonS3 s3 = new AmazonS3Client(credentialsProvider);
// Set the region of your S3 bucket
s3.setRegion(Region.getRegion(Regions.US_EAST_1));
TransferUtility transferUtility = new TransferUtility(s3, SellerHomePage.this);
TransferObserver observer = transferUtility.upload(
"willsapptest",
"plzWork",
file
);
observer.setTransferListener(new TransferListener() {
public void onStateChanged(int id, TransferState state) {
//check the state
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
public void onError(int id, Exception ex) {
Log.e("", "Error during upload: " + id, ex);
}
});
);
}catch (Exception e){
SevenSecVideoName = (TextView) findViewById(R.id.SevenSecFileNameTxt);
SevenSecVideoName.setText("File Name...");
SevenSecVideoName.setTextColor(Color.parseColor("#797979"));
e.printStackTrace();
Toast.makeText(SellerHomePage.this,"Please pick a valid video",Toast.LENGTH_SHORT).show();
}
}
}
}
I have it so unauthenticated users can upload to the S3
Error code from Android Monitor:
03-30 19:27:52.469 2255-2255/com.wilsapp.wilsapp D/CognitoCachingCredentialsProvider: Loading credentials from SharedPreferences
03-30 19:27:52.469 2255-2255/com.wilsapp.wilsapp D/CognitoCachingCredentialsProvider: No valid credentials found in SharedPreferences
03-30 19:27:53.141 2255-2255/com.wilsapp.wilsapp I/Choreographer: Skipped 33 frames! The application may be doing too much work on its main thread.
Upvotes: 0
Views: 3185
Reputation: 89
You may use it like this
Below code is used to access your aws s3 in which you have to pass accessKey and secretKey as your credentials.
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey,secret);
AmazonS3Client s3 = new AmazonS3Client(credentials);
s3.setRegion(Region.getRegion(Regions.US_EAST_1));
Transfer utility is the Class from which you can upload your file to s3.
TransferUtility transferUtility = new TransferUtility(s3, UploadFileActivity.this);
Get the path of your file from your storage and pass it as a file as below
//You have to pass your file path here.
File file = new File(filePath);
if(!file.exists()) {
Toast.makeText(UploadFileActivity.this, "File Not Found!", Toast.LENGTH_SHORT).show();
return;
}
TransferObserver observer = transferUtility.upload(
Config.BUCKETNAME,
"video_test.jpg",
file
);
Here you can use observer.setTransferListener to know the progress of your uploading file
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
if (state.COMPLETED.equals(observer.getState())) {
Toast.makeText(UploadFilesActivity.this, "File Upload Complete", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
@Override
public void onError(int id, Exception ex) {
Toast.makeText(UploadFilesActivity.this, "" + ex.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Upvotes: 2
Reputation: 932
Your CognitoCachingCredentialsProvider is not initialized correctly. You need to initialize it with the IdentityPoolId which is of the format <region>:<guid>
. You currently seem to using the identity pool name.
EDIT:
"wilsapp_MOBILEHUB_1766067928", is the name of the pool it is not the Pool id. To get the pool id go to Cognito console -> click on the name of the pool -> sample code -> copy the pool id from the sample
EDIT2: You will not get exception directly from S3 in your try catch. Instead, You can set Listener to your observer and listen t updates on upload/download
observer.setListener(new TransferListener() {
public onProgressChanged(int id, long bytesCurrent, long bytesTotal){
//update progress
}
public void onStateChanged(int id, TransferState state) {
//check the state
}
public void onError(int id, Exception ex) {
//log error
}
});
It has onError method which returns an exception to track in case there is an exception
Upvotes: 1