2

当我运行我的应用程序并使用谷歌帐户登录时,它给了我认知提供者。现在我正在尝试从应用程序将文件上传到 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 存储桶时,它没有显示该文件。没有错误,但文件也没有上传。如果您能指出我的错误,那将很有帮助。谢谢。

4

1 回答 1

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();
于 2015-03-26T23:01:47.213 回答