Image upload amazon s3 android SDK 2.0

浪尽此生 提交于 2019-12-12 11:09:26

问题


I want to upload an image to an amazon s3 bucket in android. I don't get any errors but it's just not working can anybody help me? I can't find any good examples or questions about this.

I assign a image to 'File images3'

images3 = new File(uri.getPath());

public void addEventToDB(){

        Thread thread = new Thread()
        {
            @Override
            public void run() {
                try {
                    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                            getActivity().getApplicationContext(), // get the context for the current activity
                            "...",
                            "us-east-1:...",
                            "arn:aws:iam::...:role/Cognito_WitpaAuth_DefaultRole",
                            "arn:aws:iam::...:role/Cognito_WitpaAuth_DefaultRole",
                            Regions.US_EAST_1
                    );

                    String bucket_name = "witpa";
                    String key = "images.jpeg";

                    TransferManager transferManager = new TransferManager(credentialsProvider);
                    transferManager.upload(bucket_name, key, images3);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        thread.start();

    }

I created my bucket, in the permissions I set that everyone can write and read.

In amazon cognito I just left everything as default.

Anybody knows how I can get this to work?


回答1:


Try this one. Since i had the same issue that you faced.

I have fixed by using the below code.

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentEncoding("UTF-8");
size = inputStream.available();
metadata.setContentLength(size);
TransferManager transferManager = new TransferManager(credentialsProvider);
Upload upload = transferManager.upload(bucket_name, key, images3, metadata);
upload.waitForCompletion();



回答2:


Very simple way to download image and upload image in s3 amazon. you make a simple class use this WebserviceAmazon

public class WebserviceAmazon extends AsyncTask<Void, Void, Void> {
private String mParams;
private String mResult = "x";
WebServiceInterface<String, String> mInterface;
private int mRequestType;
private  String UserId;
private Context mContext;


public WebserviceAmazon(Context context,String imagePath,String AppId,int type) {
    this.mContext = context;
    this.mParams = imagePath;
    this.mRequestType = type;
    this.UserId = AppId;
}

public void result(WebServiceInterface<String, String> myInterface) {
    this.mInterface = myInterface;
}

@Override
protected Void doInBackground(Void... params) {
    String ACCESS_KEY ="abc..";
    String SECRET_KEY = "klm...";

    try {
        if (mRequestType == 1) { // POST
            AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
            PutObjectRequest request = new PutObjectRequest("bucketName", "imageName", new File(mParams));
            s3Client.putObject(request);

            mResult = "success";
        } if (mRequestType == 2) { // For get image data
            AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
            S3Object object = s3Client.getObject(new GetObjectRequest("bucketName", mParams));
            S3ObjectInputStream objectContent = object.getObjectContent();
            byte[] byteArray = IOUtils.toByteArray(objectContent);

           Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);



            mResult = "success";
        }

    } catch (Exception e) {
        mResult = e.toString();
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}

@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
  super.onPostExecute(result);
    mInterface.success(this.mResult);

}

public interface WebServiceInterface<E, R> {
    public void success(E reslut);

    public void error(R Error);
}

}

call this webservice any where in project

    WebserviceAmazon amazon = new WebserviceAmazon(getActivity(), imageName, "", 2);
    amazon.result(new WebserviceAmazon.WebServiceInterface<String, String>() {
        @Override
        public void success(String reslut) {

        }

        @Override
        public void error(String Error) {

        }
    });

    return totalPoints;
}



回答3:


You should have to do Two Steps;

  1. Create the PutObjectRequestObject Like this:
PutObjectRequest por = new PutObjectRequest(
                              BUCKET_NAME, Key,
                              stored);
                      por.setCannedAcl(CannedAccessControlList.PublicReadWrite);

s3Client.putObject(por);
  1. Change the Policy Of Amazon s3 Server Console By Changing the bucket (Folder) Public.

I was already done that and it's working.



来源:https://stackoverflow.com/questions/27096717/image-upload-amazon-s3-android-sdk-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!