2

我正在尝试创建一项服务以在后台上传文件列表。当我定期检查MultipleFileUpload对象时,它总是处于TransferState WAITING,并且上传的字节数和要上传的总字节数总是0。在服务中运行TransferManager,或者定期检查MultipleFileUpload这种方式是否有问题?

我已经在手机和模拟器上进行了测试。当我使用 PutObjectRequest 而不是 TransferManager 时,我之前能够使用相同的 AmazonS3Client 成功上传。

这是到目前为止的代码:

// OnCreate
    @Override
    public void onCreate() {

        Log.v(TAG, "Creating Upload Service");

        ClientConfiguration clientConfig=new ClientConfiguration();
        clientConfig.setConnectionTimeout(10*1000);

        AmazonS3Client s3Client = new AmazonS3Client(
                new BasicAWSCredentials(ACCESS_KEY_ID,
                        SECRET_KEY),clientConfig);

        s3Client.setEndpoint(AMAZON_ENDPOINT);

        transferManager= new TransferManager(s3Client);

        seekIntent = new Intent(BROADCAST_ACTION);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        // Insert notification start
        initNotification();

        String[] fileNames=intent.getExtras().getStringArray(MainActivity.UPLOAD_FILE_NAMES);

        String path=intent.getExtras().getString(MainActivity.UPLOAD_FILE_PATH);

        ArrayList<File> files=new ArrayList<File>();

        for (String fileName : fileNames) {
            files.add(new File(fileName));
        }

        upload= transferManager.uploadFileList(RECORDING_BUCKET, null,new File(path),files);
        seekIntent.putExtra(UPLOAD_BYTES_TOTAL_KEY, upload.getProgress().getTotalBytesToTransfer());

        transferManager.

        setupHandler();

        return START_STICKY;
    }

    // ---Send seekbar info to activity----
    private void setupHandler() {
        handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
    }

    private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            // // Log.d(TAG, "entered sendUpdatesToUI");

            seekIntent.putExtra(UPLOAD_BYTES_TRANSFERED_KEY, upload.getProgress().getBytesTransfered());

            if(upload.isDone())
            {
                int retCode=(upload.getState()==TransferState.Completed)?0:1;

                seekIntent.putExtra(UPLOAD_COMPLETION_CODE_KEY, retCode);
            }
            else
            {
                handler.postDelayed(this, 1000); 
            }

            sendBroadcast(seekIntent);
        }
    };
4

2 回答 2

3

Turns out I had made a mistake in my understanding of the parameters for the uploadFileList method. The files I was passing in were relative to the directory file. It turns out that the files in the file list still need their full paths and the directory argument just figures out how they should be shown in the bucket. The weird thing is this did not cause an error, it just causes the behavior I mentioned in the question.

于 2013-09-22T06:09:08.510 回答
0

我发现的另一个问题是,如果使用新的 TransferUtility,则必须声明 Service

<service
android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" 
android:enabled="true" />

在您的 AndroidManifest.xml 中。这在一些但不是全部的文档中突出显示,很容易错过。

于 2015-10-20T15:33:03.343 回答