简体   繁体   English

将图像上传到AWS S3错误

[英]upload image to AWS S3 Error

I am very green for write Android Apps. 我对编写Android应用程序非常满意。 I copy some sample code to upload image to AWS S3. 我复制一些示例代码以将图像上传到AWS S3。 But when I run on phone, It always close the apps. 但是当我在手机上运行时,它总是关闭应用程序。 The below code : 下面的代码:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String mediaFile = "IMG_" + timeStamp + ".jpg";

            File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

            File tmpFile = new File(dir, mediaFile);

            final Uri outputFileUri = Uri.fromFile(tmpFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, PICK_FROM_CAMERA);

            String  ACCESS_KEY="ABCDEFGHIJKLMNOPQ",
                    SECRET_KEY="S123T456I789",
                    MY_BUCKET="Photo",

            AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
            AmazonS3 s3 = new AmazonS3Client(credentials);

TransferUtility transferUtility = new TransferUtility(s3, getApplicationContext());

TransferObserver observer = transferUtility.upload(MY_BUCKET,mediaFile,file);

Many Thanks Edmond 非常感谢Edmond

I recommend to use S3 upload by Pool Id. 我建议使用按池ID上传S3。 Using SECRET_KEY and ACCESS_KEY hardcoded in app is not recommended. 不建议在应用中使用SECRET_KEY和ACCESS_KEY硬编码。 Transfer Utility will work in same way with pool Id too You only need to Change the Credential Provider. Transfer Utility也可以与池ID以相同的方式工作。您只需要更改凭据提供程序即可。 See the below Code : 参见下面的代码:

public static TransferUtility getTransferUtility(Context context) {
    if (sTransferUtility == null) {
        sTransferUtility = new TransferUtility(getS3Client(context.getApplicationContext()),
                context.getApplicationContext());
    }
    return sTransferUtility;
}

 public static AmazonS3Client getS3Client(Context context) {
    if (sS3Client == null) {
        sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()));
        sS3Client.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
    }
    return sS3Client;
}

 private static CognitoCachingCredentialsProvider getCredProvider(Context context) {
    if (sCredProvider == null) {
        sCredProvider = new CognitoCachingCredentialsProvider(
                context.getApplicationContext(),
                Constants.COGNITO_POOL_ID,
                Regions.AP_NORTHEAST_1);
    }
    return sCredProvider;
}

Change the Region and Pool Id for your app and Use Transfer Utility to Upload : 更改您的应用程序的区域和池ID,然后使用Transfer Utility进行上传:

 TransferUtility transferUtility = AppUtil.getTransferUtility(context);
TransferObserver transferObserver = transferUtility.upload(BUCKET_NAME, forKey, new File(fileUrl));
    transferObserver.setTransferListener(new TransferListener() {
    @Override
    public void onStateChanged(int id, TransferState state) {
        if (state == TransferState.COMPLETED) {
            // Completed
        }
    }

    @Override
    public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {}

    @Override
    public void onError(int id, Exception ex) {
        ex.printStackTrace();
      // Error
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM