错误而上传文件到Amazon S3桶上传文件、错误、Amazon

2023-09-11 09:51:42 作者:★  kiyomi°

当我运行我的应用程序,并登录使用谷歌帐户,它给我的cognito提供商。现在,我想从应用程序上传文件到S3桶。首先我想从我的本地电脑上传文件,然后我会改变它从应用程序上传。这是我的code

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

这是我在我的日志。

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存储,它不显示文件。有没有错误,但该文件还没有上传。这将是有益的,如果你能指出我的错误。 谢谢你。

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.

推荐答案

我已经解决了这个问题。如果有人正在寻找方法,在这儿呢。这不是上传,因为它没有权限读取的文件。我给在AndroidManifest.xml中的权限和它的工作。

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.

权限在AndroidManifest.xml中

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

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