Upload an image from Android to Amazon S3?

前端 未结 7 2054
不知归路
不知归路 2021-02-01 06:42

I need to upload a bitmap to Amazon S3. I have never used S3, and the docs are proving less than helpful as I can\'t see anything to cover this specific requirement. Unfortunate

相关标签:
7条回答
  • 2021-02-01 07:47

    you can upload image and download 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;
    }
    
    0 讨论(0)
提交回复
热议问题