How can I save image to AWS S3 bucket and then retrieve the image public url and save the url into dynamodb alongside other user input data without the user leaving the activity or fragment? Can all these be achieved at once? Any sample code example will help a lot. Thanks!!
Try this code -
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);
}
This is how I upload images to S3:
CognitoCachingCredentialsProvider credentialsProvider =
new CognitoCachingCredentialsProvider(
context.getApplicationContext(),
YOUR_IDENTITY_POOL_ID,
Regions.US_EAST_1);
AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
TransferUtility transferUtility = new TransferUtility(s3Client, context.
getApplicationContext());
TransferObserver observer = transferUtility.upload(
MY_BUCKET, // The S3 bucket to upload to
OBJECT_KEY, // The key for the uploaded object
FILE_TO_UPLOAD // The location of the file to be uploaded );
Have this in your manifest file:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<service android:name= "com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android:enabled="true" />
I made the folder in S3 bucket public and saved the file link in dynamoDB like this: " https://mybucket.s3.amazonaws.com/myfolder/myimage.jpg "
-
Every object in S3 can have either a private or public access permissions.
If it's public then the public URL is always http(s)://s3(-region).amazonaws.com/BUCKET/KEY
so that's phase 1.
If you upload synchronously, at the end you can save meta data about that link to DynamoDB and be sure you're fixed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.