Android在本地存储来自s3存储桶的照片和文档

时间:2018-01-30 16:52:45

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

我有一个后端,我用Laravel编写,我正在编写和Android应用程序正在调用我的后端。

我的aws帐户中有一些png和pdf存储在s3存储桶中。我需要从存储桶中获取图像和文档,并将它们本地存储在设备上并显示它们。

我还需要从手机发送新的png以存储在s3存储桶中。

最好的方法是做什么。有没有有用的库。我已经添加了Picasso,但这只会显示图像无法从s3存储桶中获取/存储。

3 个答案:

答案 0 :(得分:1)

AWS有一组库可用于获取和存储在S3存储桶中。

您应该检查:Model field reference #default

答案 1 :(得分:1)

您可以使用适用于S3的AWS Android SDK。你可以通过maven在gradle中使用它:

dependencies {
  compile 'com.amazonaws:aws-android-sdk-s3:2.6.+'
}

例如,将文件上传到S3:

  import android.app.Activity;
  import android.util.Log;

  import com.amazonaws.mobile.client.AWSMobileClient;
  import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
  import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
  import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
  import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
  import com.amazonaws.services.s3.AmazonS3Client;

  import java.io.File;

  public class YourActivity extends Activity {

      public void uploadData() {

        // Initialize AWSMobileClient if not initialized upon the app startup.
        // AWSMobileClient.getInstance().initialize(this).execute();

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

        TransferObserver uploadObserver =
              transferUtility.upload(
                    "s3Folder/s3Key.txt",
                    new File("/path/to/file/localFile.txt"));

        uploadObserver.setTransferListener(new TransferListener() {

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

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

                 Log.d("MainActivity", "   ID:" + id + "   bytesCurrent: " + bytesCurrent + "   bytesTotal: " + bytesTotal + " " + percentDone + "%");
           }

           @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.
        }
    }
}

了解更多信息:

https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-user-data-storage.html#add-aws-user-data-storage-upload

https://docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-storage.html

答案 2 :(得分:1)

上传文件到s3

FALSE

下载图片

https://github.com/jontyankit/Glide-Amazon-Image-Load