简体   繁体   English

如何将图片上传到AWS s3存储桶?

[英]How to upload the picture to AWS s3 bucket?

//getting access to the photo gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
        Uri selectedImage = data.getData();
        uploadImage.setImageURI(selectedImage);
    }
    Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();

}


 //Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
//Trying to upload image to the AWS S3bucket-
public void uploadImageToAWS(View v) {
//String sdcard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "Camera/";
    Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
    File MY_FILE = new File("myPhoto.png");
    String YOUR_IDENTITY_POOL_ID ="POOL_ID";
    CognitoCachingCredentialsProvider credentialsProvider =
            new CognitoCachingCredentialsProvider(getApplicationContext(),
                    YOUR_IDENTITY_POOL_ID,
                    Regions.US_EAST_1);


    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
    s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));

    TransferUtility transferUtility = new TransferUtility(s3Client,
            getApplicationContext());

    TransferObserver observer = transferUtility.upload(
            "apptestingbucket",           // The S3 bucket to upload to
            FILE_NAME,   // The key for the uploaded object
            MY_FILE);// The location of the file to
    observer.setTransferListener(new TransferListener() {

        @Override
        public void onStateChanged(int id, TransferState state) {
            if(state == TransferState.COMPLETED){
                Toast.makeText(Social.this, "transfer Succeded!", Toast.LENGTH_LONG).show();
            }

        }

        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            int percentage = (int) (bytesCurrent/bytesTotal * 100);
            Log.e("Status", "Percentage" + percentage);

        }

        @Override
        public void onError(int id, Exception ex) {
            Log.e("MY_BUCKET", "Error: " + ex.getMessage() );

        }
    });




        }



private void retrieveImageFromAWS(){
    File sdcard = Environment.getExternalStorageDirectory();
    File MY_FILE = new File(sdcard, "social.png");
    String YOUR_IDENTITY_POOL_ID ="POOL_ID";
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(getApplicationContext(),
            YOUR_IDENTITY_POOL_ID
            , Regions.US_EAST_1);
    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
    TransferUtility transferUtility = new TransferUtility(s3Client,
            getApplicationContext());
    TransferObserver observer = transferUtility.download("BUCKET_NAME",
            "FILENAME_TO_BE_DOWNLOADED", MY_FILE);

    observer.setTransferListener(new TransferListener() {
        @Override
        public void onStateChanged(int id, TransferState state) {
            if(state == TransferState.COMPLETED){
                Toast.makeText(Social.this, "Retrieve completed", Toast.LENGTH_LONG).show();
            }

        }

        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            int percentage = (int) (bytesCurrent/bytesTotal * 100);
            Log.e("status", "percentage" +percentage);

        }

        @Override
        public void onError(int id, Exception ex) {
            Log.e("MY_BUCKET", "Error Downloading: " + ex.getMessage());


        }
    });


}

This is what i have based on the Android s3 documentation, I'm trying to pick the picture from photo gallery or take picture from the phone and upload it to S3 bucket and retrieve it back to the app interface. 这是基于Android s3文档的内容,我试图从照片库中选择图片或从手机中拍摄图片并将其上传到S3存储桶,然后将其检索回应用程序界面。 but evry time i run the programm my app crashed. 但是每次我运行程序时,我的应用程序崩溃了。 and i called 然后我打电话

  uploadImageToAWS(v)

insede the 注入

 BtnUpload.onClicklistener

any example or little help would be appreciated. 任何例子或很少的帮助将不胜感激。 thank you! 谢谢!

Try to check if you have the uses-permissions Internet in your Android Manifests file. 尝试检查您的Android Manifests文件中是否具有使用许可互联网。 If this is not working, try to see if you can upload images outside your app (eg. via command line) to your bucket. 如果这不起作用,请尝试查看是否可以将应用外部的图像(例如,通过命令行)上传到存储桶。 Maybe there is some sort of security setting you have not set properly. 也许您没有正确设置某种安全设置。 Make sure you can ping your server and set up your security settings properly. 确保可以ping服务器并正确设置安全设置。

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

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