public UploadAsync ( Stream stream, string bucketName, string key, |
||
stream | Stream | /// The stream to read to obtain the content to upload. /// |
bucketName | string | /// The target Amazon S3 bucket, that is, the name of the bucket to upload the stream to. /// |
key | string | /// The key under which the Amazon S3 object is stored. /// |
cancellationToken | /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// | |
Résultat | Task |
public async Task UploadFile(string name,IStorageFile storageFile) { var s3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1); var transferUtilityConfig = new TransferUtilityConfig { ConcurrentServiceRequests = 5, MinSizeBeforePartUpload = 20 * MB_SIZE, }; try { using (var transferUtility = new TransferUtility(s3Client, transferUtilityConfig)) { var uploadRequest = new TransferUtilityUploadRequest { BucketName = ExistingBucketName, Key = name, StorageFile = storageFile, // Set size of each part for multipart upload to 10 MB PartSize = 10 * MB_SIZE }; uploadRequest.UploadProgressEvent += OnUploadProgressEvent; await transferUtility.UploadAsync(uploadRequest); } } catch (AmazonServiceException ex) { // oResponse.OK = false; // oResponse.Message = "Network Error when connecting to AWS: " + ex.Message; } }
public async Task <string> UploadFileFromStream(Stream stream, string s3Key) { string uploadedFileUrl = string.Empty; try { using (var transferUtility = new Amazon.S3.Transfer.TransferUtility(_awsS3Client)) { using (stream) { var request = new TransferUtilityUploadRequest { BucketName = _bucketName, Key = s3Key, InputStream = stream, CannedACL = S3CannedACL.PublicRead, }; await transferUtility.UploadAsync(request); } uploadedFileUrl = bucketUrl + s3Key; } } catch (Exception ex) { throw new ApplicationException("S3 UploadFileFromStream - unexpected exception.", ex); } return(uploadedFileUrl); }
private void UploadFileToS3(string filePath,string bucketname) { var awsAccessKey = accesskey; var awsSecretKey = secretkey; var existingBucketName = bucketname; var client = Amazon.AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey, RegionEndpoint.USEast1); var uploadRequest = new TransferUtilityUploadRequest { FilePath = filePath, BucketName = existingBucketName, CannedACL = S3CannedACL.PublicRead }; var fileTransferUtility = new TransferUtility(client); fileTransferUtility.UploadAsync(uploadRequest); }
private static async System.Threading.Tasks.Task <string> UploadFile(string fileName, Stream fileStream) { var extension = Path.GetExtension(fileName); var client = new AmazonS3Client("AKIAJNOS24TJ3PWZHKEQ", "+d+qIQ5Uv8dfFTdsdvBd0Hp0Exm5QY2YH1ZL8903", RegionEndpoint.USWest2); var transfer = new Amazon.S3.Transfer.TransferUtility(client); var request = new TransferUtilityUploadRequest(); request.BucketName = "sakjfkls-test-bucket"; request.InputStream = new MemoryStream(); request.Key = Guid.NewGuid().ToString() + extension; request.InputStream = fileStream; request.CannedACL = S3CannedACL.AuthenticatedRead; await transfer.UploadAsync(request); return(request.Key); }
public static async Task UploadFile(dynamic token, string filePath) { try { string path = token.path; string accessKeyId = token.accessKeyId; string secretAccessKey = token.secretAccessKey; string sessionToken = token.sessionToken; string bucket = token.bucket; string keyName = string.Format("{0}/{1}", path, Path.GetFileName(filePath)); var client = new AmazonS3Client(accessKeyId, secretAccessKey, sessionToken, RegionEndpoint.APSoutheast2); var fileTransferUtility = new TransferUtility(client); var request = new TransferUtilityUploadRequest { BucketName = bucket, FilePath = filePath, Key = keyName, PartSize = 6291456, // 6 MB. ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256 }; await fileTransferUtility.UploadAsync(request); Trace.WriteLine(token.uploadPassword); } catch (AmazonS3Exception s3Exception) { Console.WriteLine(s3Exception.Message, s3Exception.InnerException); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public override async Task AddObject(FileProperties properties, Stream stream, bool overwrite, CancellationToken ct) { string key = ResolveLocalKey(properties.Key); var request = new TransferUtilityUploadRequest { BucketName = _bucket, InputStream = stream, Key = key }; request.AddHeader("x-amz-meta-yas4-ts", properties.Timestamp.ToString("o")); using (var util = new TransferUtility(_s3)) { await util.UploadAsync(request).ConfigureAwait(false); } }
public String UploadFile(String amazonPath, String localPath) { var transferUtility = new TransferUtility(_keyPublic, _keySecret); var uploadRequest = new TransferUtilityUploadRequest { FilePath = localPath, BucketName = _bucket, Key = amazonPath, CannedACL = new S3CannedACL("public-read"), }; var upload = transferUtility.UploadAsync(uploadRequest); upload.ContinueWith(OnUploadComplete); upload.Wait(); return GetPublicUrl(_bucket, amazonPath); }
/// <summary> /// Adds the image to the cache in an asynchronous manner. /// </summary> /// <param name="stream"> /// The stream containing the image data. /// </param> /// <param name="contentType"> /// The content type of the image. /// </param> /// <returns> /// The <see cref="Task"/> representing an asynchronous operation. /// </returns> public override async Task AddImageToCacheAsync(Stream stream, string contentType) { TransferUtility transferUtility = new TransferUtility(this.amazonS3ClientCache); string path = this.GetFolderStructureForAmazon(this.CachedPath); string filename = Path.GetFileName(this.CachedPath); string key = this.GetKey(path, filename); TransferUtilityUploadRequest transferUtilityUploadRequest = new TransferUtilityUploadRequest { BucketName = this.awsBucketName, InputStream = stream, Key = key, CannedACL = S3CannedACL.PublicRead, Headers = { CacheControl = string.Format("public, max-age={0}",this.MaxDays* 86400), ContentType = contentType } }; transferUtilityUploadRequest.Metadata.Add("x-amz-meta-ImageProcessedBy", "ImageProcessor.Web/" + AssemblyVersion); await transferUtility.UploadAsync(transferUtilityUploadRequest); }
/// <summary> /// Adds the image to the cache in an asynchronous manner. /// </summary> /// <param name="stream"> /// The stream containing the image data. /// </param> /// <param name="contentType"> /// The content type of the image. /// </param> /// <returns> /// The <see cref="Task"/> representing an asynchronous operation. /// </returns> public override async Task AddImageToCacheAsync(Stream stream, string contentType) { TransferUtility transferUtility = new TransferUtility(this.amazonS3ClientCache); string path = this.GetFolderStructureForAmazon(this.CachedPath); string filename = Path.GetFileName(this.CachedPath); string key = this.GetKey(path, filename); TransferUtilityUploadRequest transferUtilityUploadRequest = new TransferUtilityUploadRequest { BucketName = this.awsBucketName, InputStream = stream, Key = key, CannedACL = S3CannedACL.PublicRead }; await transferUtility.UploadAsync(transferUtilityUploadRequest); }
public async Task uploadFileAsyc(IcloudFileInfo fr) { //Create A New Client For every request, /* AmazonS3Config S3Config = new AmazonS3Config() { ServiceURL = "s3.amazonaws.com", RegionEndpoint = Amazon.RegionEndpoint.USEast1 }; */ try { // AmazonS3Client clientUpload = new AmazonS3Client(_settings.cloudKey1, _settings.cloudKey2, S3Config); TransferUtility fileTransferUtility = new TransferUtility(this.client); fr.CloudFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString("N") + "_" + Path.GetFileName(fr.localfileName); TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest { BucketName = (this.bucketname), FilePath = (Path.Combine(fr.localfolderName, fr.localfileName)), Key = (fr.deviceName + @"/" + fr.CloudFileName), //PartSize = (1457), CannedACL = S3CannedACL.AuthenticatedRead }; fileTransferUtilityRequest.Metadata["FileName"] = Path.GetFileName(fr.localfileName); fileTransferUtilityRequest.Metadata["DateCreated"] = fr.DateofBackup.ToString(); fileTransferUtilityRequest.Metadata["FolderName"] = fr.localfolderName; fileTransferUtilityRequest.Metadata["ModifiedDate"] = fr.localfileLastModifiedDate; fileTransferUtilityRequest.Metadata["DeviceName"] = fr.deviceName; fileTransferUtilityRequest.Metadata["OperatingSystem"] = fr.OperatingSystem; await fileTransferUtility.UploadAsync (fileTransferUtilityRequest); } catch (AmazonS3Exception err) { throw new Exception(err.Message); } catch (Exception err) { throw new Exception(err.Message); } }
//public async Task UploadFileAsync(string name, IStorageFile storageFile, FileUploadCompleted fileinfo) //{ // await Task.Factory.StartNew(() => UploadFile( name, storageFile, fileinfo)); //} public async void UploadFile(string name, IStorageFile storageFile, FileUploadCompleted fileinfo) { fileinfo.UploadStartTime = DateTime.Now; BasicAWSCredentials credentials = new BasicAWSCredentials("AKIAJTADDHY7T7GZXX5Q", "n4xV5B25mt7e6br84H2G9SXBx8eDYTQJgCxoaF49"); var s3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1); var transferUtilityConfig = new TransferUtilityConfig { ConcurrentServiceRequests = 5, MinSizeBeforePartUpload = 20 * MB_SIZE, }; try { using (var transferUtility = new TransferUtility(s3Client, transferUtilityConfig)) { var uploadRequest = new TransferUtilityUploadRequest { BucketName = ExistingBucketName, Key = name, StorageFile = storageFile, // Set size of each part for multipart upload to 10 MB PartSize = 10 * MB_SIZE, CannedACL = S3CannedACL.PublicRead }; uploadRequest.UploadProgressEvent += OnUploadProgressEvent; // fileinfo.Status = currentStatus.ToString(); try { await transferUtility.UploadAsync(uploadRequest); } catch { } // fileinfo.UploadEndtime = DateTime.Now; } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { // Console.WriteLine("Check the provided AWS Credentials."); // Console.WriteLine( // "For service sign up go to http://aws.amazon.com/s3"); } else { //Console.WriteLine( // "Error occurred. Message:'{0}' when writing an object" // , amazonS3Exception.Message); } } }