public void CopyObject(Uri sourceUri, Uri destinationUri) { CheckUri(sourceUri); CheckUri(destinationUri); try { var sourceKey = HttpUtility.UrlDecode(sourceUri.AbsolutePath).TrimStart('/'); var destinationKey = HttpUtility.UrlDecode(destinationUri.AbsolutePath).TrimStart('/'); using (var client = CreateAmazonS3Client()) { var request = new CopyObjectRequest() .WithSourceBucket(bucketName) .WithDestinationBucket(bucketName) .WithCannedACL(S3CannedACL.PublicRead) .WithSourceKey(sourceKey) .WithDestinationKey(destinationKey) .WithDirective(S3MetadataDirective.COPY); client.CopyObject(request); } } catch (Exception e) { throw new StorageException(string.Format("Failed to copy object. SourceUrl: {0}, DestinationUrl: {1}", sourceUri, destinationUri), e); } }
/// <summary> /// Copies the source content to the specified destination on the remote blob storage. /// </summary> /// <param name="source">Descriptor of the source item on the remote blob storage.</param> /// <param name="destination">Descriptor of the destination item on the remote blob storage.</param> public override void Copy(IBlobContentLocation source, IBlobContentLocation destination) { var request = new CopyObjectRequest() .WithSourceBucket(this.bucketName).WithSourceKey(source.FilePath) .WithDestinationBucket(this.bucketName).WithDestinationKey(destination.FilePath); transferUtility.S3Client.CopyObject(request); }
public override void CopyFile(string fromBin, string toBin, string fileName) { CopyObjectRequest request = new CopyObjectRequest(); request.SourceBucket = fromBin; request.DestinationBucket = toBin; request.SourceKey = fileName; request.DestinationKey = fileName; CopyObjectResponse response = client.CopyObject(request); }
public static void CopyFile(AmazonS3 s3Client, string sourcekey, string targetkey) { String destinationPath = targetkey; CopyObjectRequest request = new CopyObjectRequest() { SourceBucket = BUCKET_NAME, SourceKey = sourcekey, DestinationBucket = BUCKET_NAME, DestinationKey = targetkey }; CopyObjectResponse response = s3Client.CopyObject(request); }
/// <summary> /// The copy file. /// </summary> /// <param name="awsAccessKey"> /// The AWS access key. /// </param> /// <param name="awsSecretKey"> /// The AWS secret key. /// </param> /// <param name="sourceBucket"> /// The source bucket. /// </param> /// <param name="sourceKey"> /// The source key. /// </param> /// <param name="destinationBucket"> /// The destination bucket. /// </param> /// <param name="destinationKey"> /// The destination key. /// </param> public static void CopyFile(string awsAccessKey, string awsSecretKey, string sourceBucket, string sourceKey, string destinationBucket, string destinationKey) { using (var amazonClient = AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey)) { var copyRequest = new CopyObjectRequest { CannedACL = S3CannedACL.PublicRead }; copyRequest.WithSourceBucket(sourceBucket); copyRequest.WithSourceKey(sourceKey); copyRequest.WithDestinationBucket(destinationBucket); copyRequest.WithDestinationKey(destinationKey); amazonClient.CopyObject(copyRequest); } }
public void Copy(string copyFrom, string copyTo) { try { var copyRequest = new CopyObjectRequest().WithSourceBucket(this._bucketName).WithDestinationBucket(this._bucketName).WithSourceKey(copyFrom).WithDestinationKey(copyTo).WithCannedACL(S3CannedACL.PublicReadWrite); this._client.CopyObject(copyRequest); } catch (Exception ex) { Log.Error(string.Format("Cannot copy file from {0} to {1}. Debug message: {2}", copyFrom, copyTo, ex.Message)); return; } }
/// <summary> /// Copies the source content to the specified destination on the remote blob storage. /// </summary> /// <param name="source">Descriptor of the source item on the remote blob storage.</param> /// <param name="destination">Descriptor of the destination item on the remote blob storage.</param> public override void Copy(IBlobContentLocation source, IBlobContentLocation destination) { var request = new CopyObjectRequest() { SourceBucket = this.bucketName, SourceKey = source.FilePath, DestinationBucket = this.bucketName, DestinationKey = destination.FilePath, CannedACL = S3CannedACL.PublicRead }; transferUtility.S3Client.CopyObject(request); }
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public async Task <CopyObjectResponse> CopyObjectAsync(string sourceBucket, string sourceKey, string destinationBucket, string destinationKey, CancellationToken cancellationToken = default) { this.Logger.LogDebug($"[{nameof(this.CopyObjectAsync)}]"); this.Logger.LogTrace(JsonConvert.SerializeObject(new { sourceBucket, sourceKey, destinationBucket, destinationKey })); if (string.IsNullOrWhiteSpace(sourceBucket)) { throw new ArgumentNullException(nameof(sourceBucket)); } if (string.IsNullOrWhiteSpace(sourceKey)) { throw new ArgumentNullException(nameof(sourceKey)); } if (string.IsNullOrWhiteSpace(destinationBucket)) { throw new ArgumentNullException(nameof(destinationBucket)); } if (string.IsNullOrWhiteSpace(destinationKey)) { throw new ArgumentNullException(nameof(destinationKey)); } var request = new Amazon.S3.Model.CopyObjectRequest { SourceBucket = sourceBucket, DestinationBucket = destinationBucket, SourceKey = sourceKey, DestinationKey = destinationKey, }; this.Logger.LogTrace(JsonConvert.SerializeObject(value: request)); var response = await this.Repository.CopyObjectAsync(request : request, cancellationToken : cancellationToken == default?this.CancellationToken.Token : cancellationToken); this.Logger.LogTrace(JsonConvert.SerializeObject(value: response)); return(response); }
public void CopyFile(string srcDomain, string srcPath, string destDomain, string destPath) { var srcKey = GetKey(_srcTenant, _srcModuleConfiguration.Name, srcDomain, srcPath); var destKey = GetKey(_destTenant, _destModuleConfiguration.Name, destDomain, destPath); using (var s3 = GetS3Client()) { var copyRequest = new CopyObjectRequest{ SourceBucket = _srcBucket, SourceKey = srcKey, DestinationBucket = _destBucket, DestinationKey = destKey, CannedACL = GetDestDomainAcl(destDomain), Directive = S3MetadataDirective.REPLACE, ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256 }; s3.CopyObject(copyRequest); } }
public virtual async Task<bool> Move(string oldNamePath, string newNamePath) { bool completed = false; try { IAmazonS3 client = GetS3Client(); CopyObjectRequest request = new CopyObjectRequest() { SourceBucket = Settings.BucketName, SourceKey = oldNamePath, DestinationBucket = Settings.BucketName, DestinationKey = newNamePath, CannedACL = S3CannedACL.PublicRead }; CopyObjectResponse response = await client.CopyObjectAsync(request); DeleteObjectRequest deleteRequest = new DeleteObjectRequest() { BucketName = request.SourceBucket, Key = request.SourceKey }; DeleteObjectResponse deleteResponse = await client.DeleteObjectAsync(deleteRequest); completed = true; } catch (AmazonS3Exception ex) { _logger.Exception(ex); } return completed; }
private void RenameObject(string oldPath, string newPath, IAmazonS3 client) { CopyObjectRequest copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = BucketName; copyRequest.SourceKey = oldPath; copyRequest.DestinationBucket = BucketName; copyRequest.DestinationKey = newPath; copyRequest.CannedACL = S3CannedACL.PublicRead; client.CopyObject(copyRequest); }
IAsyncResult invokeCopyObject(CopyObjectRequest copyObjectRequest, AsyncCallback callback, object state, bool synchronized) { IRequest irequest = new CopyObjectRequestMarshaller().Marshall(copyObjectRequest); var unmarshaller = CopyObjectResponseUnmarshaller.GetInstance(); AsyncResult result = new AsyncResult(irequest, callback, state, synchronized, signer, unmarshaller); Invoke(result); return result; }
/// <summary> /// Initiates the asynchronous execution of the CopyObject operation. /// <seealso cref="Amazon.S3.IAmazonS3.CopyObject"/> /// </summary> /// /// <param name="copyObjectRequest">Container for the necessary parameters to execute the CopyObject operation on AmazonS3.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndCopyObject /// operation.</returns> public IAsyncResult BeginCopyObject(CopyObjectRequest copyObjectRequest, AsyncCallback callback, object state) { return invokeCopyObject(copyObjectRequest, callback, state, false); }
/// <summary> /// <para>Creates a copy of an object that is already stored in Amazon S3.</para> /// </summary> /// /// <param name="copyObjectRequest">Container for the necessary parameters to execute the CopyObject service method on AmazonS3.</param> /// /// <returns>The response from the CopyObject service method, as returned by AmazonS3.</returns> /// public CopyObjectResponse CopyObject(CopyObjectRequest copyObjectRequest) { IAsyncResult asyncResult = invokeCopyObject(copyObjectRequest, null, null, true); return EndCopyObject(asyncResult); }
/// <summary> /// Initiates the asynchronous execution of the CopyObject operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CopyObject operation on AmazonS3Client.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndCopyObject /// operation.</returns> public IAsyncResult BeginCopyObject(CopyObjectRequest request, AsyncCallback callback, object state) { var marshaller = new CopyObjectRequestMarshaller(); var unmarshaller = CopyObjectResponseUnmarshaller.Instance; return BeginInvoke<CopyObjectRequest>(request, marshaller, unmarshaller, callback, state); }
public void RenameFolder(long objectId, string oldObjectName, string newObjectName, string clientDateTime2) { var userData = _readOnlyRepository.First<Account>(a => a.EMail == User.Identity.Name); var fileData = userData.Files.FirstOrDefault(f => f.Id == objectId); var userFiles = userData.Files.Where(t => t.Url.Contains(fileData.Name)); var clientDate = Convert.ToDateTime(clientDateTime2); var newFoldUrl = string.IsNullOrEmpty(fileData.Url) || string.IsNullOrWhiteSpace(fileData.Url) ? newObjectName + "/" : fileData.Url.Replace(oldObjectName, newObjectName) + fileData.Name + "/"; var putFolder = new PutObjectRequest { BucketName = userData.BucketName, Key = newFoldUrl, ContentBody = string.Empty }; AWSClient.PutObject(putFolder); foreach (var file in userFiles) { if (file == null) continue; if (file.IsDirectory) { RenameFolder(file.Id, oldObjectName, newObjectName, clientDateTime2); } else { //Copy the object var newUrl = file.Url.Replace(oldObjectName, newObjectName) + file.Name; var copyRequest = new CopyObjectRequest { SourceBucket = userData.BucketName, SourceKey = file.Url + file.Name, DestinationBucket = userData.BucketName, DestinationKey = newUrl, CannedACL = S3CannedACL.PublicRead }; AWSClient.CopyObject(copyRequest); //Delete the original var deleteRequest = new DeleteObjectRequest { BucketName = userData.BucketName, Key = file.Url + file.Name }; AWSClient.DeleteObject(deleteRequest); file.ModifiedDate = clientDate; file.Url = file.Url.Replace(oldObjectName, newObjectName); _writeOnlyRepository.Update(file); } }//fin foreach var deleteFolderRequest = new DeleteObjectRequest { BucketName = userData.BucketName, Key = fileData.Url + fileData.Name + "/" }; AWSClient.DeleteObject(deleteFolderRequest); var newFolderUrl = fileData.Url.Replace(oldObjectName, newObjectName); fileData.Url = newFolderUrl; _writeOnlyRepository.Update(fileData); }
public void CopyFile(string fromVirtualPath, string destinationVirtualPath) { var copyRequest = new CopyObjectRequest() .WithMetaData("Expires", DateTime.Now.AddYears(10).ToString("R")) .WithSourceBucket(this.bucketName) .WithSourceKey(fromVirtualPath) .WithDestinationBucket(this.bucketName) .WithDestinationKey(destinationVirtualPath) .WithCannedACL(S3CannedACL.PublicRead); using (this.s3.CopyObject(copyRequest)) { } if (FileCopied != null) FileCopied.Invoke(this, new FileEventArgs(FixPathForN2(fromVirtualPath), FixPathForN2(destinationVirtualPath))); }
internal CopyObjectResponse CopyObject(CopyObjectRequest request) { var marshaller = new CopyObjectRequestMarshaller(); var unmarshaller = CopyObjectResponseUnmarshaller.Instance; return Invoke<CopyObjectRequest,CopyObjectResponse>(request, marshaller, unmarshaller); }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { if (sClass > S3StorageClass.ReducedRedundancy || sClass < S3StorageClass.Standard) { throw new ArgumentException("Invalid value specified for storage class."); } if (null == s3Client) { throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests."); } // Get the existing ACL of the object GetACLRequest getACLRequest = new GetACLRequest(); getACLRequest.BucketName = bucketName; getACLRequest.Key = key; if(version != null) getACLRequest.VersionId = version; GetACLResponse getACLResponse = s3Client.GetACL(getACLRequest); // Set the storage class on the object CopyObjectRequest copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; if (version != null) copyRequest.SourceVersionId = version; copyRequest.StorageClass = sClass; // The copyRequest's Metadata directive is COPY by default CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. SetACLRequest setACLRequest = new SetACLRequest(); setACLRequest.BucketName = bucketName; setACLRequest.Key = key; if (version != null) setACLRequest.VersionId = copyResponse.VersionId; setACLRequest.ACL = getACLResponse.AccessControlList; s3Client.SetACL(setACLRequest); }
/// <summary> /// Creates a copy of an object that is already stored in Amazon S3. /// </summary> /// <param name="sourceBucket">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="sourceKey">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="sourceVersionId">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="destinationBucket">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="destinationKey">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// /// <returns>The response from the CopyObject service method, as returned by S3.</returns> public Task<CopyObjectResponse> CopyObjectAsync(string sourceBucket, string sourceKey, string sourceVersionId, string destinationBucket, string destinationKey, System.Threading.CancellationToken cancellationToken = default(CancellationToken)) { var request = new CopyObjectRequest(); request.SourceBucket = sourceBucket; request.SourceKey = sourceKey; request.SourceVersionId = sourceVersionId; request.DestinationBucket = destinationBucket; request.DestinationKey = destinationKey; return CopyObjectAsync(request, cancellationToken); }
/// <summary> /// Creates a copy of an object that is already stored in Amazon S3. /// </summary> /// <param name="sourceBucket">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="sourceKey">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="sourceVersionId">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="destinationBucket">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="destinationKey">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// /// <returns>The response from the CopyObject service method, as returned by S3.</returns> public CopyObjectResponse CopyObject(string sourceBucket, string sourceKey, string sourceVersionId, string destinationBucket, string destinationKey) { var request = new CopyObjectRequest(); request.SourceBucket = sourceBucket; request.SourceKey = sourceKey; request.SourceVersionId = sourceVersionId; request.DestinationBucket = destinationBucket; request.DestinationKey = destinationKey; return CopyObject(request); }
public void ReplaceAdImages(ref Ad ad, FileName[] filenames) { string newFileName = ""; int count = 1; var id = ad.Id; var imaa = db.AdImages.Where(x => x.adId.Equals(id)).Count(); count = imaa + 1; for (int i = 1; i < filenames.Length; i++) { IAmazonS3 client; try { using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { GetObjectRequest request = new GetObjectRequest { BucketName = _bucketName, Key = _folderName + filenames[i].fileName }; using (GetObjectResponse response = client.GetObject(request)) { string filename = filenames[i].fileName; if (!System.IO.File.Exists(filename)) { string extension = System.IO.Path.GetExtension(filenames[i].fileName); newFileName = ad.Id.ToString() + "_" + count + extension; client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); CopyObjectRequest request1 = new CopyObjectRequest() { SourceBucket = _bucketName, SourceKey = _folderName + filename, DestinationBucket = _bucketName, CannedACL = S3CannedACL.PublicRead,//PERMISSION TO FILE PUBLIC ACCESIBLE DestinationKey = _folderName + newFileName }; CopyObjectResponse response1 = client.CopyObject(request1); AdImage image = new AdImage(); image.imageExtension = extension; image.adId = ad.Id; db.AdImages.Add(image); db.SaveChanges(); count++; DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest { BucketName = _bucketName, Key = _folderName + filenames[i].fileName }; AmazonS3Config config = new AmazonS3Config(); config.ServiceURL = "https://s3.amazonaws.com/"; using (client = Amazon.AWSClientFactory.CreateAmazonS3Client( _awsAccessKey, _awsSecretKey, config)) { client.DeleteObject(deleteObjectRequest); } } } } } catch (Exception e) { } } }
/// <summary> /// Sets up the request needed to make an exact copy of the object leaving the parent method /// the ability to change just the attribute being requested to change. /// </summary> /// <param name="bucketName"></param> /// <param name="key"></param> /// <param name="version"></param> /// <param name="s3Client"></param> /// <param name="copyRequest"></param> /// <param name="setACLRequest"></param> static void SetupForObjectModification(string bucketName, string key, string version, AmazonS3 s3Client, out CopyObjectRequest copyRequest, out SetACLRequest setACLRequest) { // Get the existing ACL of the object GetACLRequest getACLRequest = new GetACLRequest(); getACLRequest.BucketName = bucketName; getACLRequest.Key = key; if (version != null) getACLRequest.VersionId = version; GetACLResponse getACLResponse = s3Client.GetACL(getACLRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. setACLRequest = new SetACLRequest(); setACLRequest.BucketName = bucketName; setACLRequest.Key = key; setACLRequest.ACL = getACLResponse.AccessControlList; ListObjectsResponse listObjectResponse = s3Client.ListObjects(new ListObjectsRequest() .WithBucketName(bucketName) .WithPrefix(key) .WithMaxKeys(1)); if (listObjectResponse.S3Objects.Count != 1) { throw new ArgumentNullException("No object exists with this bucket name and key."); } GetObjectMetadataRequest getMetaRequest = new GetObjectMetadataRequest() { BucketName = bucketName, Key = key }; GetObjectMetadataResponse getMetaResponse = s3Client.GetObjectMetadata(getMetaRequest); // Set the storage class on the object copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; copyRequest.StorageClass = listObjectResponse.S3Objects[0].StorageClass == "STANDARD" ? S3StorageClass.Standard : S3StorageClass.ReducedRedundancy; if (version != null) copyRequest.SourceVersionId = version; copyRequest.WebsiteRedirectLocation = getMetaResponse.WebsiteRedirectLocation; copyRequest.ServerSideEncryptionMethod = getMetaResponse.ServerSideEncryptionMethod; }
// Para renombrar un archivo public bool Post([FromUri] string token, [FromUri] long objectId, [FromUri] string newName) { var userData = CheckPermissions(token); var fileData = _readOnlyRepository.GetById<File>(objectId); var clientDate = DateTime.Now; if (!fileData.IsDirectory) { //Copy the object var copyRequest = new CopyObjectRequest { SourceBucket = userData.BucketName, SourceKey = fileData.Url + fileData.Name, DestinationBucket = userData.BucketName, DestinationKey = fileData.Url + newName + "." + (fileData.Name.Split('.').LastOrDefault()), CannedACL = S3CannedACL.PublicRead }; AWSClient.CopyObject(copyRequest); //Delete the original var deleteRequest = new DeleteObjectRequest { BucketName = userData.BucketName, Key = fileData.Url + fileData.Name }; AWSClient.DeleteObject(deleteRequest); fileData.ModifiedDate = clientDate; fileData.Name = newName + "." + (fileData.Name.Split('.').LastOrDefault()); _writeOnlyRepository.Update(fileData); return true; } else { RenameFolder(objectId, fileData.Name, newName, clientDate.ToString()); fileData.ModifiedDate = clientDate; fileData.Name = newName; _writeOnlyRepository.Update(fileData); return true; } }
protected override void ProcessRecord() { AmazonS3 client = base.GetClient(); Amazon.S3.Model.CopyObjectRequest request = new Amazon.S3.Model.CopyObjectRequest(); request.SourceBucket = this._SourceBucket; request.SourceKey = this._SourceKey; request.DestinationBucket = this._DestinationBucket; request.DestinationKey = this._DestinationKey; request.ContentType = this._ContentType; request.ETagToMatch = this._ETagToMatch; request.ETagToNotMatch = this._ETagToNotMatch; request.ModifiedSinceDate = this._ModifiedSinceDate; request.UnmodifiedSinceDate = this._UnmodifiedSinceDate; request.Timeout = this._Timeout; request.SourceVersionId = this._SourceVersionId; Amazon.S3.Model.CopyObjectResponse response = client.CopyObject(request); }
/// <summary> /// Creates a copy of an object that is already stored in Amazon S3. /// This API is supported only when AWSConfigs.HttpClient is set to AWSConfigs.HttpClientOption.UnityWebRequest, the default value of this configuration option is AWSConfigs.HttpClientOption.UnityWWW /// </summary> /// <param name="sourceBucket">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="sourceKey">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="sourceVersionId">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="destinationBucket">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="destinationKey">A property of CopyObjectRequest used to execute the CopyObject service method.</param> /// <param name="callback">An Action delegate that is invoked when the operation completes.</param> /// <param name="options"> /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// </param> /// /// <returns>The response from the CopyObject service method, as returned by S3.</returns> public void CopyObjectAsync(string sourceBucket, string sourceKey, string sourceVersionId, string destinationBucket, string destinationKey, AmazonServiceCallback<CopyObjectRequest, CopyObjectResponse> callback, AsyncOptions options = null) { var request = new CopyObjectRequest(); request.SourceBucket = sourceBucket; request.SourceKey = sourceKey; request.SourceVersionId = sourceVersionId; request.DestinationBucket = destinationBucket; request.DestinationKey = destinationKey; CopyObjectAsync(request, callback, options); }
public virtual async Task<bool> Copy(List<string> oldNamePaths, List<string> newNamePaths) { bool completed = false; try { IAmazonS3 client = GetS3Client(); List<Task<CopyObjectResponse>> requestTasks = new List<Task<CopyObjectResponse>>(); for (int i = 0; i < oldNamePaths.Count; i++) { CopyObjectRequest request = new CopyObjectRequest() { SourceBucket = Settings.BucketName, SourceKey = oldNamePaths[i], DestinationBucket = Settings.BucketName, DestinationKey = newNamePaths[i], CannedACL = S3CannedACL.PublicRead }; Task<CopyObjectResponse> response = client.CopyObjectAsync(request); requestTasks.Add(response); } CopyObjectResponse[] responses = await Task.WhenAll(requestTasks.ToArray()); completed = true; } catch (AmazonS3Exception ex) { _logger.Exception(ex); } return completed; }
/// <summary> /// Copies this file to the location indicated by the passed in S3FileInfo. /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown. /// </summary> /// <param name="file">The target location to copy this file to.</param> /// <param name="overwrite">Determines whether the file can be overwritten.</param> /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception> /// <exception cref="T:System.Net.WebException"></exception> /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception> /// <returns>S3FileInfo of the newly copied file.</returns> public S3FileInfo CopyTo(S3FileInfo file, bool overwrite) { if (!overwrite) { if (file.Exists) { throw new IOException("File already exists"); } } if (SameClient(file)) { var request = new CopyObjectRequest { DestinationBucket = file.BucketName, DestinationKey = S3Helper.EncodeKey(file.ObjectKey), SourceBucket = bucket, SourceKey = S3Helper.EncodeKey(key) }; request.BeforeRequestEvent += S3Helper.FileIORequestEventHandler; s3Client.CopyObject(request); } else { var getObjectRequest = new GetObjectRequest { BucketName = bucket, Key = S3Helper.EncodeKey(key) }; getObjectRequest.BeforeRequestEvent += S3Helper.FileIORequestEventHandler; var getObjectResponse = s3Client.GetObject(getObjectRequest); using (Stream stream = getObjectResponse.ResponseStream) { var putObjectRequest = new PutObjectRequest { BucketName = file.BucketName, Key = S3Helper.EncodeKey(file.ObjectKey), InputStream = stream }; putObjectRequest.BeforeRequestEvent += S3Helper.FileIORequestEventHandler; file.S3Client.PutObject(putObjectRequest); } } return file; }
/// <summary> /// Initiates the asynchronous execution of the CopyObject operation. /// This API is supported only when AWSConfigs.HttpClient is set to AWSConfigs.HttpClientOption.UnityWebRequest, the default value for this configuration option is AWSConfigs.HttpClientOption.UnityWWW /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CopyObject operation on AmazonS3Client.</param> /// <param name="callback">An Action delegate that is invoked when the operation completes.</param> /// <param name="options">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> public void CopyObjectAsync(CopyObjectRequest request, AmazonServiceCallback<CopyObjectRequest, CopyObjectResponse> callback, AsyncOptions options = null) { if (AWSConfigs.HttpClient == AWSConfigs.HttpClientOption.UnityWWW) { throw new InvalidOperationException("CopyObject is only allowed with AWSConfigs.HttpClientOption.UnityWebRequest API option"); } options = options == null?new AsyncOptions():options; var marshaller = new CopyObjectRequestMarshaller(); var unmarshaller = CopyObjectResponseUnmarshaller.Instance; Action<AmazonWebServiceRequest, AmazonWebServiceResponse, Exception, AsyncOptions> callbackHelper = null; if(callback !=null ) callbackHelper = (AmazonWebServiceRequest req, AmazonWebServiceResponse res, Exception ex, AsyncOptions ao) => { AmazonServiceResult<CopyObjectRequest,CopyObjectResponse> responseObject = new AmazonServiceResult<CopyObjectRequest,CopyObjectResponse>((CopyObjectRequest)req, (CopyObjectResponse)res, ex , ao.State); callback(responseObject); }; BeginInvoke<CopyObjectRequest>(request, marshaller, unmarshaller, options, callbackHelper); }
/// <summary> /// Initiates the asynchronous execution of the CopyObject operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CopyObject operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task<CopyObjectResponse> CopyObjectAsync(CopyObjectRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new CopyObjectRequestMarshaller(); var unmarshaller = CopyObjectResponseUnmarshaller.Instance; return InvokeAsync<CopyObjectRequest,CopyObjectResponse>(request, marshaller, unmarshaller, cancellationToken); }
public void RenameFile(string bucketName, string source, string target) { CopyObjectRequest copyObjectRequest = new CopyObjectRequest(); copyObjectRequest.SourceBucket = bucketName; copyObjectRequest.SourceKey = source; copyObjectRequest.DestinationBucket = bucketName; copyObjectRequest.DestinationKey = target; using (CopyObjectResponse copyObjectResponse = m_client.CopyObject(copyObjectRequest)) { } DeleteObject(bucketName, source); }