在将文件上传到Android中的AWS S3时,文件名“@”字符将替换为“%40”

时间:2018-02-28 18:22:37

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

我正在开发一个Android应用程序。在我的应用程序中,我将文件上传到AWS S3存储桶。我现在可以使用AWS Android SDK将文件成功上传到s3存储桶。但问题是文件名中的“@”字符在s3 Bucket上被替换为“%40”。

这是我上传文件到S3的代码

   public void s3credentialsProvider()
   {
      CognitoCachingCredentialsProvider cognitoCachingCredentialsProvider = new CognitoCachingCredentialsProvider(
         this.context,
         AwsUtility.COGNITO_POOL_ID,
         AwsUtility.BUCKET_REGION
      );
      createAmazonS3Client(cognitoCachingCredentialsProvider   );
   }


   public void createAmazonS3Client(CognitoCachingCredentialsProvider
                                       credentialsProvider){

      if(s3Client==null)
      {
         // Create an S3 client
         s3Client = new AmazonS3Client(credentialsProvider);

         // Set the region of your S3 bucket
         s3Client.setRegion(Region.getRegion(AwsUtility.BUCKET_REGION));
      }
   }

   public void setTransferUtility(){

      transferUtility = new TransferUtility(s3Client, context);
   }

   public CachedFile uploadFileTos3(CachedFile cachedFile)
   {

      File file = new File(Utilities.getRealPathFromUri(context, Uri.parse(String.valueOf(cachedFile.getFilePath()))));
      String fileHash = Utilities.getFileMD5Hash(file);
      cachedFile.setFileHash(fileHash);

      String extension = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("."));
      String fileId = cachedFile.getEventCode() + "/" + cachedFile.getEmail().replace("%40","@") + "/" + cachedFile.getFileHash()  + extension;
      try{
         //fileId = URLEncoder.encode(fileId, "utf-8");
         TransferObserver transferObserver = transferUtility.upload(AwsUtility.BUCKET_NAME, fileId, file);
         transferObserverListener(transferObserver);
      }
      catch(Exception e)
      {

      }
      cachedFile.setFileId(fileId);

      return cachedFile;

   }

   public void transferObserverListener(TransferObserver transferObserver){

      transferObserver.setTransferListener(new TransferListener(){

         @Override
         public void onStateChanged(int id, TransferState state) {
            Log.i("SYNC_STATE", "State Change" + state);
         }

         @Override
         public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            int percentage = (int) (bytesCurrent/bytesTotal * 100);
            Log.i("SYNC_PROGRESS", "Progress in %" + percentage);
         }

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

      });
   }

我按以下顺序调用方法:

s3credentialsProvide
 setTransferUtility
 uploadFileTos3

正在上传到S3。但问题是S3上的文件名:

TransferObserver transferObserver = transferUtility.upload(AwsUtility.BUCKET_NAME, fileId, file);

如果我为fileId传递了这个值“myemail@gmail.com/img-1.jpg”,那么该文件就变成了“myemail%40gmail.com / img-1.jpg”。我不希望@字符被替换。我该如何解决这个问题?

0 个答案:

没有答案