我将AWS Android SDK添加到我的应用程序中,并且我在AsyncTask中有一些代码可以将我的视频上传到我的存储桶,但它无效。
Android代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
try {
Uri selectedImageUri = data.getData();
File file = new File(String.valueOf(selectedImageUri));
// Initialize the Amazon Cognito credentials provider
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"us-east-1:5bfc6b1a-6193-4fXc-823f-ba21a9fadc1b", // Identity Pool ID
Regions.US_EAST_1 // Region
);
// Create an S3 client
AmazonS3 s3 = new AmazonS3Client(credentialsProvider);
// Set the region of your S3 bucket
s3.setRegion(Region.getRegion(Regions.US_EAST_1));
TransferUtility transferUtility = new TransferUtility(s3, SellerHomePage.this);
TransferObserver observer = transferUtility.upload(
"willsapptest",
"plzWork",
file
);
observer.setTransferListener(new TransferListener() {
public void onStateChanged(int id, TransferState state) {
//check the state
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
public void onError(int id, Exception ex) {
Log.e("", "Error during upload: " + id, ex);
}
});
);
}catch (Exception e){
SevenSecVideoName = (TextView) findViewById(R.id.SevenSecFileNameTxt);
SevenSecVideoName.setText("File Name...");
SevenSecVideoName.setTextColor(Color.parseColor("#797979"));
e.printStackTrace();
Toast.makeText(SellerHomePage.this,"Please pick a valid video",Toast.LENGTH_SHORT).show();
}
}
}
}
我有这么认可的用户可以上传到S3
Android Monitor的错误代码:
03-30 19:27:52.469 2255-2255/com.wilsapp.wilsapp D/CognitoCachingCredentialsProvider: Loading credentials from SharedPreferences
03-30 19:27:52.469 2255-2255/com.wilsapp.wilsapp D/CognitoCachingCredentialsProvider: No valid credentials found in SharedPreferences
03-30 19:27:53.141 2255-2255/com.wilsapp.wilsapp I/Choreographer: Skipped 33 frames! The application may be doing too much work on its main thread.
答案 0 :(得分:1)
您的CognitoCachingCredentialsProvider未正确初始化。您需要使用格式为<region>:<guid>
的IdentityPoolId对其进行初始化。您当前似乎使用标识池名称。
编辑:
“wilsapp_MOBILEHUB_1766067928”,是池的名称,它不是池ID。要获取池ID,请转到Cognito控制台 - &gt;点击泳池名称 - &gt;示例代码 - &gt;从示例中复制池ID
EDIT2: 在try catch中,您不会直接从S3获得异常。代替, 您可以将Listener设置为您的观察者,并在上传/下载时收听更新
observer.setListener(new TransferListener() {
public onProgressChanged(int id, long bytesCurrent, long bytesTotal){
//update progress
}
public void onStateChanged(int id, TransferState state) {
//check the state
}
public void onError(int id, Exception ex) {
//log error
}
});
它有onError方法,在发生异常时返回跟踪异常
答案 1 :(得分:1)
您可以像这样使用它
下面的代码用于访问您的aws s3,您必须在其中传递accessKey和secretKey作为您的凭据。
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey,secret);
AmazonS3Client s3 = new AmazonS3Client(credentials);
s3.setRegion(Region.getRegion(Regions.US_EAST_1));
传输实用程序是您可以将文件上传到s3的类。
TransferUtility transferUtility = new TransferUtility(s3, UploadFileActivity.this);
从存储中获取文件的路径,并将其作为文件传递,如下所示
//You have to pass your file path here.
File file = new File(filePath);
if(!file.exists()) {
Toast.makeText(UploadFileActivity.this, "File Not Found!", Toast.LENGTH_SHORT).show();
return;
}
TransferObserver observer = transferUtility.upload(
Config.BUCKETNAME,
"video_test.jpg",
file
);
您可以使用observer.setTransferListener来了解上传文件的进度
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
if (state.COMPLETED.equals(observer.getState())) {
Toast.makeText(UploadFilesActivity.this, "File Upload Complete", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
@Override
public void onError(int id, Exception ex) {
Toast.makeText(UploadFilesActivity.this, "" + ex.getMessage(), Toast.LENGTH_SHORT).show();
}
});