如何将图像保存到aws s3 bucket中,然后检索图像公共url并将url与其他用户输入数据一起保存到dynamodb中,而不让用户留下活动或片段?
所有这些都能同时实现吗?
任何示例代码示例都会有很大帮助。谢谢!!
最佳答案
尝试此代码-
private void uploadImageToAWS() {
AsyncTask<String, String, String> _Task = new AsyncTask<String, String, String>() {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0)
{
if (NetworkAvailablity.checkNetworkStatus(PrintRollScreenActivity.this))
{
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 = "YOUR_AWS_BUCKET_NAME";////////////
String keyName = "image_name";
String filePath = "";
AmazonS3Client s3Client1 = new AmazonS3Client( new BasicAWSCredentials( "AWS_KEY","AWS_SECRET") );
PutObjectRequest por = new PutObjectRequest(existingBucketName,
keyName + ".png",new File(filePath));//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();
}
}
else
{
}
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)
{
}
};
_Task.execute((String[]) null);
}