将文件上载到Amazon S3存储桶时出错

时间:2015-03-04 18:17:09

标签: android amazon-web-services amazon-s3 amazon-iam awss3transfermanager

当我运行我的应用并使用Google帐户登录时,它会向我提供认知提供程序。现在我正在尝试从应用程序将文件上传到S3存储桶。首先,我尝试从我的本地笔记本电脑上传文件,然后我将其更改为从应用程序上传。这是我的代码

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");}

这就是我在日志中获得的内容。

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

现在,当我检查AWS网站上的S3存储桶时,它不会显示该文件。没有错误,但文件也没有上传。如果你能指出我的错误会很有帮助。 感谢。

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。如果有人在寻找这种方法,那么就是这样。它没有上传,因为它没有读取文件的权限。我在AndroidManifest.xml中提供了权限,但它确实有效。

AndroidManifest.xml中的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

工作代码

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();