繁体   English   中英

将视频从Android上传到AWS S3

[英]upload video from Android to AWS S3

我将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.

您可以这样使用

下面的代码用于访问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();
                }
            });

您的CognitoCachingCredentialsProvider未正确初始化。 您需要使用IdentityPoolId初始化它,其格式为<region>:<guid> 您当前似乎正在使用身份池名称。

编辑:

“ wilsapp_MOBILEHUB_1766067928”是池的名称,而不是池ID。 要获取池ID,请转到Cognito控制台->单击池的名称->示例代码->从示例中复制池ID

EDIT2:您不会在尝试捕获中直接从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方法,该方法会在出现异常的情况下返回要跟踪的异常

暂无
暂无

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

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