gpt4 book ai didi

android - AWS Android SDK 元数据支持问题

转载 作者:行者123 更新时间:2023-11-30 02:45:58 25 4
gpt4 key购买 nike

所以我有一个简单的 Android 应用程序可以将文件上传到 AWS S3 存储桶,我正在寻找如何使用 AWS 的元数据支持将文件名存储在对象 URL 中。

我怎么会这样做?

下面是我的MainActivity.java文件:

package com.example.s3imagetest;

import java.net.URL;
import java.util.Date;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.OpenableColumns;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.ResponseHeaderOverrides;

public class MainActivity extends Activity {

private AmazonS3Client s3Client = new AmazonS3Client(
new BasicAWSCredentials(Constants.ACCESS_KEY_ID,
Constants.SECRET_KEY));

private Button selectPhoto = null;
private Button showInBrowser = null;

private static final int PHOTO_SELECTED = 1;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.v("OnCreate", "OnCreate Was Called");

s3Client.setRegion(Region.getRegion(Regions.EU_WEST_1));


setContentView(R.layout.activity_main);

selectPhoto = (Button) findViewById(R.id.button1);
selectPhoto.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// Start the image picker.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PHOTO_SELECTED);
}
});

showInBrowser = (Button) findViewById(R.id.button2);
showInBrowser.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
new S3GeneratePresignedUrlTask().execute();
}
});
}

// This method is automatically called by the image picker when an image is
// selected.

protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

switch (requestCode) {
case PHOTO_SELECTED:
if(resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
new S3PutObjectTask().execute(selectedImage);
}
}
}

// Display an Alert message for error or failure
protected void displayAlert(String title, String message) {
AlertDialog.Builder confirm = new AlertDialog.Builder(this);
confirm.setTitle(title);
confirm.setMessage(message);

confirm.setNegativeButton(MainActivity.this.getString(R.string.ok),
new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
confirm.show().show();
}

protected void displayErrorAlert(String title, String message) {
AlertDialog.Builder confirm = new AlertDialog.Builder(this);
confirm.setTitle(title);
confirm.setMessage(message);

confirm.setNegativeButton(MainActivity.this.getString(R.string.ok),
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
});
confirm.show().show();
}

private class S3PutObjectTask extends AsyncTask<Uri, Void, S3TaskResult> {
ProgressDialog dialog;

protected void onPreExecute() {
Log.v("S3PutObjectTask", "Its Beginning ");
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage(MainActivity.this.getString(R.string.uploading));
dialog.setCancelable(false);
dialog.show();
Log.v("S3PutObjectTask", "onPreExecute Done");
}


protected S3TaskResult doInBackground(Uri... uris) {
Log.v("S3TaskResult", "Uri" + uris);
if (uris == null || uris.length != 1) {
return null;
}

// The file location of the image selected.
Uri selectedImage = uris[0];

ContentResolver resolver = getContentResolver();
String fileSizeColumn[] = { OpenableColumns.SIZE };

Cursor cursor = resolver.query(selectedImage, fileSizeColumn, null,
null, null);

cursor.moveToFirst();

int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
String size = null;
if (!cursor.isNull(sizeIndex)) {
size = cursor.getString(sizeIndex);
}

cursor.close();

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(resolver.getType(selectedImage));

if (size != null) {
metadata.setContentLength(Long.parseLong(size));
}

S3TaskResult result = new S3TaskResult();

// Put the image data into S3.
try {
Log.v("PutData into S3", "Data has bee sent");
//s3Client.createBucket(Constants.getPictureBucket());

PutObjectRequest por = new PutObjectRequest(
Constants.PICTURE_BUCKET, Constants.PICTURE_NAME,
resolver.openInputStream(selectedImage), metadata);
Log.v("PubtObjectRequest", "Selected Image");
s3Client.putObject(por);
Log.v("S3Client.putOb", "por");
Toast.makeText(getApplicationContext(), "Photo Upload Success", Toast.LENGTH_LONG).show();

} catch (Exception exception) {

result.setErrorMessage(exception.getMessage());
}

return result;
}

protected void onPostExecute(S3TaskResult result) {

dialog.dismiss();

if (result.getErrorMessage() != null) {

displayErrorAlert(
MainActivity.this
.getString(R.string.upload_failure_title),
result.getErrorMessage());
}
}
}

private class S3GeneratePresignedUrlTask extends
AsyncTask<Void, Void, S3TaskResult> {

protected S3TaskResult doInBackground(Void... voids) {

S3TaskResult result = new S3TaskResult();

try {
// Ensure that the image will be treated as such.
Log.v("S3GeneratePresignedUrlTask", "S3TaskResult");
ResponseHeaderOverrides override = new ResponseHeaderOverrides();
override.setContentType("image/jpeg");

// Generate the pre-signed URL.

// Added an hour's worth of milliseconds to the current time.
Date expirationDate = new Date(
System.currentTimeMillis() + 3600000);
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
Constants.PICTURE_BUCKET, Constants.PICTURE_NAME);
urlRequest.setExpiration(expirationDate);
urlRequest.setResponseHeaders(override);

URL url = s3Client.generatePresignedUrl(urlRequest);

result.setUri(Uri.parse(url.toURI().toString()));

} catch (Exception exception) {

result.setErrorMessage(exception.getMessage());
}

return result;
}

protected void onPostExecute(S3TaskResult result) {

if (result.getErrorMessage() != null) {

displayErrorAlert(
MainActivity.this
.getString(R.string.browser_failure_title),
result.getErrorMessage());
} else if (result.getUri() != null) {

// Display in Browser.
startActivity(new Intent(Intent.ACTION_VIEW, result.getUri()));
}
}
}

private class S3TaskResult {
String errorMessage = null;
Uri uri = null;

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

public Uri getUri() {
return uri;
}

public void setUri(Uri uri) {
this.uri = uri;
}
}
}

最佳答案

来自 http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/

setUserMetadata(Map) 似乎就是您要找的东西,尽管我认为这不是解决此问题的最佳方法。

Sets the custom user-metadata for the associated object.

Amazon S3 can store additional metadata on objects by internally representing it as HTTP headers prefixed with "x-amz-meta-". Use user-metadata to store arbitrary metadata alongside their data in Amazon S3. When setting user metadata, callers should not include the internal "x-amz-meta-" prefix; this library will handle that for them. Likewise, when callers retrieve custom user-metadata, they will not see the "x-amz-meta-" header prefix.

User-metadata keys are case insensitive and will be returned as lowercase strings, even if they were originally specified with uppercase strings.

Note that user-metadata for an object is limited by the HTTP request header limit. All HTTP headers included in a request (including user metadata headers and other standard HTTP headers) must be less than 8KB.

根据您的描述,这听起来像是一款用户可以上传照片并通过浏览器和手机查看照片的应用程序。但是,您希望当用户从浏览器中删除照片时,手机上相应的本地副本也将被删除。

我认为您可以使用 setUserMetadata 方法通过传递描述路径的元数据来完成此操作。然而,这将非常麻烦,因为元数据只是小写,不幸的是路径和文件名不能保证是(我认为?)。因此,您需要自己的方式将小写字符串映射到大写和小写字符串,这会增加很多不必要的复杂性。

我觉得如果您设置一个数据库来保存从 S3 文件名到它们各自的本地路径的映射会更好。您可以在 Android 设备上执行此操作,这样会容易得多,但如果用户尝试数据,可能会出现奇怪的行为。您也可以在自己的后端执行此操作,如果您要向用户发送推送通知以无论如何处理删除操作,这可能更有意义,因为您可以将路径推送给他们。

一个更大的潜在问题是您必须区分设备,尽管您可以通过在表中保留与每个设备关联的唯一 ID 来解决这个问题。还有当用户决定移动图片时该怎么做的问题。

无论如何,这些东西与手头的问题并不真正相关。要回答您的问题,您可以对元数据执行此操作,但不建议这样做,因为它增加了很多复杂性,但似乎没有什么好处。如果您正在寻找最简单的方法(处理所有这些客户端),那么我可能会建议您尝试使用 Cognito 和 DynamoDB 从您的应用程序直接向某个 DynamoDB 表添加一行。确保您指定了正确的权限,既允许您执行此操作,也不允许用户在采取这种方法时获得任何额外的权力。

希望其中一些有所帮助!

关于android - AWS Android SDK 元数据支持问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25019016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com