我已经实现了以下代码来上传S3上的图像。

时间:2016-10-10 15:52:38

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

从图库中获取图像后,我正在调用以下函数,当我运行程序时,它运行时没有错误但仍然无法在我的S3存储桶中找到该图像。这是我的函数

private void uploadImageToAWS(){

    final AsyncTask<String, String, String> _Task = new AsyncTask<String, String, String>() {

        @Override
        protected void onPreExecute() {


        }



        @Override
        protected String doInBackground(String... arg0)
        {

            if (CheckNetStatus())
            {
                try {
                    java.util.Date expiration = new java.util.Date();
                    long msec = expiration.getTime();
                    msec += 1000 * 60 * 60; // 1 hour.
                    expiration.setTime(msec);
                    publishProgress(arg0);

                    String existingBucketName = "nkupload";
                    String keyName = "001";
                 // String filePath = pathstring;

                    AmazonS3Client s3Client1 = new AmazonS3Client( new BasicAWSCredentials( access_key_id,secret_access_key) );
                    PutObjectRequest por = new PutObjectRequest(existingBucketName,
                            keyName + ".png",new File(pictureDirectoryPath));//key is  URL

                    //making the object Public
                    por.setCannedAcl(CannedAccessControlList.PublicRead);
                    s3Client1.putObject(por);


                    String _finalUrl = "https://"+existingBucketName+".s3.amazonaws.com/" + keyName + ".png";

                } catch (Exception e) {
                    // writing error to Log
                    e.printStackTrace();
                    Toast bread = Toast.makeText(getApplicationContext(), "An Error Occured", Toast.LENGTH_LONG);
                    bread.show();

                }




            }
            else
            {
                Toast bread = Toast.makeText(getApplicationContext(), "An Error Occured", Toast.LENGTH_LONG);
                bread.show();

            }


            return null;

        }
        @Override
        protected void onProgressUpdate(String... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            System.out.println("Progress : "  + values);
        }
        @Override
        protected void onPostExecute(String result)
        {
            Toast bread = Toast.makeText(getApplicationContext(), "An Error Occured", Toast.LENGTH_LONG);
            bread.show();
        }
    };


    _Task.execute((String[]) null);


}

1 个答案:

答案 0 :(得分:0)

这是我在上传功能doInBackGround中添加的内容,它正常工作并返回上传图片的新路径

AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
            s3Client.setRegion(Region.getRegion(Regions.EU_CENTRAL_1));
            PutObjectRequest por = new PutObjectRequest(bucketName, folderName + "/" + fileName, new File(filePath));
            s3Client.putObject(por);
            return s3Client.getResourceUrl(bucketName, folderName + "/" + fileName);