s3 TransferUtility无法将对象内容存储到磁盘:xxxxxx(权限被拒绝)

时间:2018-07-17 08:15:24

标签: amazon-s3 permissions denied

当前我正在做一个Android应用程序,它将s3中的json文件提取到我的应用程序中,但是目前我面临着一种非常奇怪的情况。

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

这是我的清单

 AWSMobileClient.getInstance().initialize(this).execute();
 downloadWithTransferUtility();

 private void downloadWithTransferUtility() {
    String KEY = "HFKJHUWW";
    String SECRET = "WDHUIWHDUIWHUI";

    BasicAWSCredentials credentials = new BasicAWSCredentials(KEY, SECRET);
    AmazonS3Client s3Client = new AmazonS3Client(credentials);

    TransferUtility transferUtility =
            TransferUtility.builder()
                    .context(getApplicationContext())
                    .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
                    .s3Client(s3Client)
                    .build();

    String fileName = "movieDb.JSON";
    // "private" will be the folder that contains the file
    String location = "/storage/";
    File localFile = new File(location + fileName);

    //If File is not present create directory
    if (!localFile.exists()) {
        localFile.mkdir();
        Log.e("localFile", "Directory Created.");
    }

    TransferObserver downloadObserver =
            transferUtility.download("sgmovie-userfiles-mobilehub-2034921464","movieData/" + fileName, localFile);

    // Attach a listener to the observer to get state update and progress notifications
    downloadObserver.setTransferListener(new TransferListener() {

        @Override
        public void onStateChanged(int id, TransferState state) {
            if (TransferState.COMPLETED == state) {
                Log.e("Download", "Download successful.");
            }
        }

        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            float percentDonef = ((float)bytesCurrent/(float)bytesTotal) * 100;
            int percentDone = (int)percentDonef;
        }

        @Override
        public void onError(int id, Exception ex) {
            Log.e("Download fail", ex.getMessage());
        }

    });
}

我得到E / Download失败:无法将对象内容存储到磁盘:/storage/movieDb.JSON(权限被拒绝)。

我尝试通过提示来寻求许可

    public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

对这个问题感到绝望,任何人都可以帮助您?太谢谢了!! 顺便说一句,奇怪的是我有其他写文件功能正常工作, 是因为transferUtility吗?

0 个答案:

没有答案