S b*_*uce 2 url android amazon-s3 amazon-web-services amazon-dynamodb
如何将图像保存到AWS S3存储桶,然后检索图像公共网址并将网址与其他用户输入数据一起保存到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);
}
Run Code Online (Sandbox Code Playgroud)
这是我将图像上传到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 );
Run Code Online (Sandbox Code Playgroud)
在您的清单文件中有这个:
<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" />
Run Code Online (Sandbox Code Playgroud)
我在S3 bucket public中创建了文件夹并保存了dynamoDB中的文件链接:" https://mybucket.s3.amazonaws.com/myfolder/myimage.jpg "
-
| 归档时间: |
|
| 查看次数: |
10445 次 |
| 最近记录: |