问题
When I run my app and logins with google account, it gives me the cognito provider. Now I am trying to upload a file to S3 bucket from the app. First I am trying to upload a file from my local laptop, then I will change it to upload from the app. Here is my code
provider = new CognitoCachingCredentialsProvider(mContext,
AWS_ACCOUNT_ID, IDENTITY_POOL_ID, UNAUTH_ROLE_ARN, AUTH_ROLE_ARN,Regions.EU_WEST_1);
client = new CognitoSyncManager(mContext, IDENTITY_POOL_ID, Regions.EU_WEST_1, provider);
String BUCKET_NAME = "uni-cloud";
String access_key = "something";
TransferManager transferManager = new TransferManager(provider);
File file = new File("E:\\Google Drive\\Year 3\\Project\\dummy.docx");
Log.e("Cognito Provider ID","Data " + provider.getIdentityId());
try {
Upload upload = transferManager.upload(BUCKET_NAME,access_key, file);
while (!upload.isDone()){
Log.i("upload","Uploading");
}
Log.i("upload","Uploaded");
}catch(Exception e) {Log.i("Upload", "Error while uploading");}
This is what I get in my logs.
03-04 17:27:57.789 24584-24712/com.unicloud.mittal I/upload﹕ Uploading
03-04 17:27:57.789 24584-24712/com.unicloud.mittal I/upload﹕ Uploading
03-04 17:27:57.799 24584-24712/com.unicloud.mittal I/upload﹕ Uploaded
Now when I check the S3 bucket on AWS site, it doesn't show the file. There are no errors but the file is also not uploaded. It would be helpful if you can point out my mistake. Thanks.
回答1:
I have solved this issue. If someone is looking for the method, here it is. It was not uploading because it didn't have permission to read the file. I gave the permissions in AndroidManifest.xml and it worked.
Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Working code
TransferManager transferManager = new TransferManager(provider);
String bucket = "uni-cloud";
File file = new File("//sdcard//Download//cw.pdf");
if(file.exists())
{
Log.e(TAG,"File found " + file.getName());
}
else {
Log.e(TAG,"File not found");
}
Upload upload = transferManager.upload(bucket, file.getName(), file);
while (!upload.isDone()){
//Show a progress bar...
TransferProgress transferred = upload.getProgress();
Toast.makeText(this, "Uploading... ", Toast.LENGTH_LONG).show();
Log.i("Percentage", "" +transferred.getPercentTransferred());
}
Toast.makeText(this, "Uploaded", Toast.LENGTH_LONG).show();
来源:https://stackoverflow.com/questions/28861887/error-while-uploading-file-to-amazon-s3-bucket