尝试在后台将文件从Windows 8.1上传到Amazon s3。请建议我该怎么办。 我正在使用XAML。使用Amazon S3的BackgroundDownloader和BackgroundUploader的代码片段将是一个很好的帮助。 请在下面找到它的部分实现。
public async Task UploadFile(IReadOnlyList<StorageFile> files)
{
basicAwsCredentials = new BasicAWSCredentials(accesskey,secretkey);
List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>();
for (int i = 0; i < files.Count; i++)
{
BackgroundTransferContentPart part = new BackgroundTransferContentPart("File" + i, files[i].Name);
part.SetFile(files[i]);
parts.Add(part);
}
Uri uri = new Uri(bucketurl+ExistingBucketName+"/");
BackgroundUploader uploader = new BackgroundUploader();
UploadOperation upload = await uploader.CreateUploadAsync(uri, parts);
// Attach progress and completion handlers.
await HandleUploadAsync(upload, true);
}
private async Task HandleUploadAsync(UploadOperation upload, bool start)
{
try
{
CancellationTokenSource cts = new CancellationTokenSource();
Progress<UploadOperation> progressCallback = new Progress<UploadOperation>();
if (start)
{
// Start the upload and attach a progress handler.
await upload.StartAsync().AsTask(cts.Token, progressCallback);
}
else
{
// The upload was already running when the application started, re-attach the progress handler.
await upload.AttachAsync().AsTask(cts.Token, progressCallback);
}
ResponseInformation response = upload.GetResponseInformation();
// Log(String.Format("Completed: {0}, Status Code: {1}", upload.Guid, response.StatusCode));
}
catch (TaskCanceledException)
{
// Log("Upload cancelled.");
}
catch (Exception ex)
{
//LogException("Error", ex);
}
}