繁体   English   中英

无法在AWS S3上上传相机图像文件

[英]Failed to upload camera image file on aws s3

无法将图像路径上传到android中的s3

content:/com.earlysalary.android.provider/external_files/IMG_20180523_164228_1998498766.jpg

我正在使用FileProvider获取相机图像的uri。 尝试在uri以上的S3上上传时,文件无效。

String authorities = mContext.getPackageName();
Uri  imgUri = FileProvider.getUriForFile(getContext(), authorities +".provider", photoFile);

为了在s3上上传,我正在使用

 File uploadFile = new File(imgUri .getPath());

   TransferObserver transferObserver = transferUtility.upload("my_bucket", bucketFilePath, uploadFile, CannedAccessControlList.PublicRead);{

}

出错

System.err: java.lang.IllegalArgumentException: Invalid file: content:/com.example.android.provider/external_files/IMG_20180523_164228_1998498766.jpg
05-23 16:43:45.931 30128-30128/com.earlysalary.android W/System.err:     at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:532)
05-23 16:43:45.931 30128-30128/com.earlysalary.android W/System.err:     at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:497)
05-23 16:43:45.931 30128-30128/com.earlysalary.android W/System.err:     at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:436)

确保文件路径有效且不为null。 另外,请检查访问s3存储桶的正确权限。 通常,此过程很简单。 在此处查看链接: http : //javasampleapproach.com/android/uploaddownload-files-images-amazon-s3-android

在上面的链接上,如果转到第5.4节“上载文件”,则在代码中交叉检查您是否设置了正确的TransferObserver。 上方链接的代码段如下:

// KEY and SECRET are gotten when we create an IAM user above
BasicAWSCredentials credentials = new BasicAWSCredentials(KEY, SECRET);
AmazonS3Client s3Client = new AmazonS3Client(credentials);

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

// "jsaS3" will be the folder that contains the file
TransferObserver uploadObserver =
        transferUtility.upload("jsaS3/" + fileName, file);

uploadObserver.setTransferListener(new TransferListener() {

   @Override
   public void onStateChanged(int id, TransferState state) {
      if (TransferState.COMPLETED == state) {
         // Handle a completed download.
      }
   }

   @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) {
      // Handle errors
   }

});

// If your upload does not trigger the onStateChanged method inside your
// TransferListener, you can directly check the transfer state as shown here.
if (TransferState.COMPLETED == uploadObserver.getState()) {
   // Handle a completed upload.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM