public static AmazonS3Client SetupBlob(string container, string path)
        {
            var request = new ListVersionsRequest
            {
                BucketName = container,
                Prefix = path
            };

            var resp = _client.ListVersions(request);
            var toDelete = resp.Versions.Where(v => v.Key == path).Select(v => new KeyVersion
            {
                Key = v.Key,
                VersionId = v.VersionId
            }).ToList();

            if (toDelete.Any())
            {
                var delRequest = new DeleteObjectsRequest
                {
                    BucketName = container,
                    Objects = toDelete,
                    Quiet = true
                };

                _client.DeleteObjects(delRequest);
            }

            return _client;
        }
        public object Execute(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;
            // create request
            var request = new Amazon.S3.Model.ListVersionsRequest();

            if (cmdletContext.BucketName != null)
            {
                request.BucketName = cmdletContext.BucketName;
            }
            if (cmdletContext.Delimiter != null)
            {
                request.Delimiter = cmdletContext.Delimiter;
            }
            if (cmdletContext.KeyMarker != null)
            {
                request.KeyMarker = cmdletContext.KeyMarker;
            }
            if (cmdletContext.MaxKey != null)
            {
                request.MaxKeys = cmdletContext.MaxKey.Value;
            }
            if (cmdletContext.Prefix != null)
            {
                request.Prefix = cmdletContext.Prefix;
            }
            if (cmdletContext.VersionIdMarker != null)
            {
                request.VersionIdMarker = cmdletContext.VersionIdMarker;
            }
            if (cmdletContext.Encoding != null)
            {
                request.Encoding = cmdletContext.Encoding;
            }

            CmdletOutput output;

            // issue call
            var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);

            try
            {
                var    response       = CallAWSServiceOperation(client, request);
                object pipelineOutput = null;
                pipelineOutput = cmdletContext.Select(response, this);
                output         = new CmdletOutput
                {
                    PipelineOutput  = pipelineOutput,
                    ServiceResponse = response
                };
            }
            catch (Exception e)
            {
                output = new CmdletOutput {
                    ErrorResponse = e
                };
            }

            return(output);
        }
Beispiel #3
0
        /// <summary>
        /// <para>Returns metadata about all of the versions of objects in a bucket.</para>
        /// </summary>
        /// 
        /// <param name="request">Container for the necessary parameters to execute the ListVersions service method on
        /// AmazonS3.</param>
        /// 
        /// <returns>The response from the ListVersions service method, as returned by AmazonS3.</returns>
		public ListVersionsResponse ListVersions(ListVersionsRequest request)
        {
            var task = ListVersionsAsync(request);
            try
            {
                return task.Result;
            }
            catch(AggregateException e)
            {
                ExceptionDispatchInfo.Capture(e.InnerException).Throw();
                return null;
            }
        }
 private void DeleteObjectVersions(string key)
 {
     try
     {
         var versionsRequest = new ListVersionsRequest
                                   {
                                       BucketName = Utilities.MyConfig.BucketKey,
                                       Prefix = key
                                   };
         var result = _amazons3.ListVersions(versionsRequest);
         foreach (S3ObjectVersion s3ObjectVersion in result.Versions)
         {
             try
             {
                 var deleteObjectRequest = new Amazon.S3.Model.DeleteObjectRequest
                                               {
                                                   BucketName = Utilities.MyConfig.BucketKey,
                                                   Key = key,
                                                   VersionId = s3ObjectVersion.VersionId
                                               };
                 _amazons3.DeleteObject(deleteObjectRequest);
             }
             catch (Exception)
             {
                 // ToDo
                 return;
             }
         }
     }
     catch (Exception)
     {
         // ToDo
         return;
     }
 }
 private void SetAcltoObject(string key)
 {
     try
     {
         var versionsRequest = new ListVersionsRequest
                                   {
                                       BucketName = Utilities.MyConfig.BucketKey,
                                       Prefix = key
                                   };
         var result = _amazons3.ListVersions(versionsRequest);
         foreach (S3ObjectVersion s3ObjectVersion in result.Versions)
         {
             if (!s3ObjectVersion.IsDeleteMarker)
             {
                 try
                 {
                     // Get ACL.
                     var getRequest = new GetACLRequest { BucketName = Utilities.MyConfig.BucketKey, Key = key, VersionId = s3ObjectVersion.VersionId };
                     GetACLResponse getResponse = _amazons3.GetACL(getRequest);
                     if (getResponse.AccessControlList.Grants.Count < 2)
                     {
                         S3AccessControlList acl = getResponse.AccessControlList;
                         getResponse.Dispose();
                         //acl.Grants.Clear();
                         //var grantee0 = new S3Grantee();
                         //grantee0.WithCanonicalUser(acl.Owner.Id, acl.Owner.DisplayName);
                         //acl.AddGrant(grantee0, S3Permission.FULL_CONTROL);
                         var grantee1 = new S3Grantee();
                         grantee1.WithURI("http://acs.amazonaws.com/groups/global/AllUsers");
                         acl.AddGrant(grantee1, S3Permission.READ);
                         var request = new SetACLRequest
                                           {
                                               BucketName = Utilities.MyConfig.BucketKey,
                                               ACL = acl,
                                               Key = key,
                                               VersionId = s3ObjectVersion.VersionId
                                           };
                         SetACLResponse response = _amazons3.SetACL(request);
                         response.Dispose();
                     }
                 }
                 catch (Exception)
                 {
                     // Todo
                     return;
                 }
             }
         }
     }
     catch (Exception)
     {
         return;
     }
 }
 protected override void ProcessRecord()
 {
     AmazonS3 client = base.GetClient();
     Amazon.S3.Model.ListVersionsRequest request = new Amazon.S3.Model.ListVersionsRequest();
     request.BucketName = this._BucketName;
     request.Prefix = this._Prefix;
     request.KeyMarker = this._KeyMarker;
     request.VersionIdMarker = this._VersionIdMarker;
     request.MaxKeys = this._MaxKeys;
     request.Delimiter = this._Delimiter;
     Amazon.S3.Model.ListVersionsResponse response = client.ListVersions(request);
 }
 public static bool CheckVersion(bool forceCheck)
 {
     // first check the last updated date
     _notification = new Notification();
     _notification.SetMessage("Checking for updates");
     try
     {
         if (forceCheck)
         {
             _notification.Show();
             _notification.ShowForm(10);
         }
         if (forceCheck)
         {
             var amazons3 = AWSClientFactory.CreateAmazonS3Client(Utilities.AwsAccessKey, Utilities.AwsSecretKey, new AmazonS3Config { CommunicationProtocol = Protocol.HTTP });
             var listVersionRequest = new ListVersionsRequest { BucketName = Utilities.AppRootBucketName, Prefix = "VersaVaultSyncTool_32Bit.exe" };
             foreach (var s3ObjectVersion in amazons3.ListVersions(listVersionRequest).Versions)
             {
                 if (s3ObjectVersion.IsLatest)
                 {
                     if (!string.IsNullOrEmpty(Utilities.MyConfig.InstallerVersionId) && Utilities.MyConfig.InstallerVersionId != s3ObjectVersion.VersionId)
                     {
                         while (Process.GetProcessesByName("VersaVaultSyncTool_32Bit").Length != 0)
                         {
                             Thread.Sleep(1000);
                             Application.DoEvents();
                         }
                         if (File.Exists(Path.Combine(Path.GetTempPath(), "VersaVaultSyncTool_32Bit_old.exe")))
                             File.Delete(Path.Combine(Path.GetTempPath(), "VersaVaultSyncTool_32Bit_old.exe"));
                         if (File.Exists(Path.Combine(Path.GetTempPath(), "VersaVaultSyncTool_32Bit.exe")))
                         {
                             File.Move(Path.Combine(Path.GetTempPath(), "VersaVaultSyncTool_32Bit.exe"),
                                       Path.Combine(Path.GetTempPath(), "VersaVaultSyncTool_32Bit_old.exe"));
                         }
                         var service = new S3Service { AccessKeyID = Utilities.AwsAccessKey, SecretAccessKey = Utilities.AwsSecretKey };
                         _notification.SetMessage("Started downloading update.");
                         service.GetObjectProgress += ServiceGetObjectProgress;
                         service.GetObject(Utilities.AppRootBucketName, s3ObjectVersion.Key, Path.Combine(Path.GetTempPath(), "VersaVaultSyncTool_32Bit.exe"));
                         _notification.SetMessage("Updating VersaVault");
                         Utilities.MyConfig.InstallerVersionId = s3ObjectVersion.VersionId;
                         Utilities.MyConfig.Save();
                         var startInfo = new ProcessStartInfo(Path.Combine(Path.GetTempPath(), "VersaVaultSyncTool_32Bit.exe")) { Verb = "runas" };
                         Process.Start(startInfo);
                         Application.Exit();
                         return false;
                     }
                     Utilities.MyConfig.InstallerVersionId = s3ObjectVersion.VersionId;
                     Utilities.MyConfig.Save();
                     break;
                 }
             }
             listVersionRequest = new ListVersionsRequest { BucketName = Utilities.AppRootBucketName, Prefix = "VersaVaultSyncTool.exe" };
             foreach (var s3ObjectVersion in amazons3.ListVersions(listVersionRequest).Versions)
             {
                 if (s3ObjectVersion.IsLatest)
                 {
                     if (s3ObjectVersion.VersionId != null && s3ObjectVersion.VersionId != "null")
                     {
                         if (!string.IsNullOrEmpty(Utilities.MyConfig.VersionId) && Utilities.MyConfig.VersionId != s3ObjectVersion.VersionId)
                         {
                             _notification.Dispose();
                             Utilities.MyConfig.VersionId = s3ObjectVersion.VersionId;
                             Utilities.MyConfig.Save();
                             var startInfo = new ProcessStartInfo(Path.Combine(Application.StartupPath, "VersaVaultUpdater.exe"), s3ObjectVersion.VersionId + " " + "update") { Verb = "runas" };
                             Process.Start(startInfo);
                             Application.Exit();
                             return false;
                         }
                         Utilities.MyConfig.VersionId = s3ObjectVersion.VersionId;
                         Utilities.MyConfig.Save();
                         return true;
                     }
                     // enable bucker versioning
                     var setBucketVersioning = new SetBucketVersioningRequest { BucketName = Utilities.AppRootBucketName, VersioningConfig = new S3BucketVersioningConfig { Status = "Enabled" } };
                     amazons3.SetBucketVersioning(setBucketVersioning);
                     break;
                 }
             }
             return true;
         }
     }
     catch (Exception)
     {
     }
     finally
     {
         try
         {
             _notification.Controls["LblStatus"].Text = @"VersaVault is upto date.";
             _notification.HideForm(10);
             _notification.Close();
             _notification.Dispose();
         }
         catch (Exception)
         {
         }
     }
     return true;
 }
        /// <summary>
        /// <para>Returns metadata about all of the versions of objects in a bucket.</para>
        /// </summary>
        /// 
        /// <param name="listVersionsRequest">Container for the necessary parameters to execute the ListVersions service method on
        /// AmazonS3.</param>
        /// 
        /// <returns>The response from the ListVersions service method, as returned by AmazonS3.</returns>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
		public async Task<ListVersionsResponse> ListVersionsAsync(ListVersionsRequest listVersionsRequest, CancellationToken cancellationToken = default(CancellationToken))
        {
            var marshaller = new ListVersionsRequestMarshaller();
            var unmarshaller = ListVersionsResponseUnmarshaller.GetInstance();
            var response = await Invoke<IRequest, ListVersionsRequest, ListVersionsResponse>(listVersionsRequest, marshaller, unmarshaller, signer, cancellationToken)
                .ConfigureAwait(continueOnCapturedContext: false);
            return response;
        }
 /// <summary>
 /// Paginator for ListVersions operation
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public IListVersionsPaginator ListVersions(ListVersionsRequest request)
 {
     return(new ListVersionsPaginator(this._client, request));
 }
        /// <summary>
        /// Initiates the asynchronous execution of the ListVersions operation.
        /// </summary>
        /// 
        /// <param name="request">Container for the necessary parameters to execute the ListVersions 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<ListVersionsResponse> ListVersionsAsync(ListVersionsRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken))
        {
            var marshaller = new ListVersionsRequestMarshaller();
            var unmarshaller = ListVersionsResponseUnmarshaller.Instance;

            return InvokeAsync<ListVersionsRequest,ListVersionsResponse>(request, marshaller, 
                unmarshaller, cancellationToken);
        }
 IAsyncResult invokeListVersions(ListVersionsRequest listVersionsRequest, AsyncCallback callback, object state, bool synchronized)
 {
     IRequest irequest = new ListVersionsRequestMarshaller().Marshall(listVersionsRequest);
     var unmarshaller = ListVersionsResponseUnmarshaller.GetInstance();
     AsyncResult result = new AsyncResult(irequest, callback, state, synchronized, signer, unmarshaller);
     Invoke(result);
     return result;
 }
 /// <summary>
 /// <para>Returns metadata about all of the versions of objects in a bucket.</para>
 /// </summary>
 /// 
 /// <param name="listVersionsRequest">Container for the necessary parameters to execute the ListVersions service method on
 ///          AmazonS3.</param>
 /// 
 /// <returns>The response from the ListVersions service method, as returned by AmazonS3.</returns>
 /// 
 public ListVersionsResponse ListVersions(ListVersionsRequest listVersionsRequest)
 {
     IAsyncResult asyncResult = invokeListVersions(listVersionsRequest, null, null, true);
     return EndListVersions(asyncResult);
 }
 /// <summary>
 /// Initiates the asynchronous execution of the ListVersions operation.
 /// <seealso cref="Amazon.S3.IAmazonS3.ListVersions"/>
 /// </summary>
 /// 
 /// <param name="listVersionsRequest">Container for the necessary parameters to execute the ListVersions 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
 ///         EndListVersions operation.</returns>
 public IAsyncResult BeginListVersions(ListVersionsRequest listVersionsRequest, AsyncCallback callback, object state)
 {
     return invokeListVersions(listVersionsRequest, callback, state, false);
 }
Beispiel #14
0
 /// <summary>
 /// Returns metadata about all of the versions of objects in a bucket.
 /// </summary>
 /// <param name="bucketName">A property of ListVersionsRequest used to execute the ListVersions service method.</param>
 /// <param name="prefix">Limits the response to keys that begin with the specified prefix.</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 ListVersions service method, as returned by S3.</returns>
 public void ListVersionsAsync(string bucketName, string prefix,  AmazonServiceCallback<ListVersionsRequest, ListVersionsResponse> callback, AsyncOptions options = null)
 {
     var request = new ListVersionsRequest();
     request.BucketName = bucketName;
     request.Prefix = prefix;
     ListVersionsAsync(request, callback, options);
 }
Beispiel #15
0
 /// <summary>
 /// Initiates the asynchronous execution of the ListVersions operation.
 /// </summary>
 /// 
 /// <param name="request">Container for the necessary parameters to execute the ListVersions 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 ListVersionsAsync(ListVersionsRequest request, AmazonServiceCallback<ListVersionsRequest, ListVersionsResponse> callback, AsyncOptions options = null)
 {
     options = options == null?new AsyncOptions():options;
     var marshaller = new ListVersionsRequestMarshaller();
     var unmarshaller = ListVersionsResponseUnmarshaller.Instance;
     Action<AmazonWebServiceRequest, AmazonWebServiceResponse, Exception, AsyncOptions> callbackHelper = null;
     if(callback !=null )
         callbackHelper = (AmazonWebServiceRequest req, AmazonWebServiceResponse res, Exception ex, AsyncOptions ao) => { 
             AmazonServiceResult<ListVersionsRequest,ListVersionsResponse> responseObject 
                     = new AmazonServiceResult<ListVersionsRequest,ListVersionsResponse>((ListVersionsRequest)req, (ListVersionsResponse)res, ex , ao.State);    
                 callback(responseObject); 
         };
     BeginInvoke<ListVersionsRequest>(request, marshaller, unmarshaller, options, callbackHelper);
 }
 private Amazon.S3.Model.ListVersionsResponse CallAWSServiceOperation(IAmazonS3 client, Amazon.S3.Model.ListVersionsRequest request)
 {
     Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Simple Storage Service (S3)", "ListVersions");
     try
     {
         #if DESKTOP
         return(client.ListVersions(request));
         #elif CORECLR
         return(client.ListVersionsAsync(request).GetAwaiter().GetResult());
         #else
                 #error "Unknown build edition"
         #endif
     }
     catch (AmazonServiceException exc)
     {
         var webException = exc.InnerException as System.Net.WebException;
         if (webException != null)
         {
             throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
         }
         throw;
     }
 }
 internal ListVersionsPaginator(IAmazonS3 client, ListVersionsRequest request)
 {
     this._client  = client;
     this._request = request;
 }
Beispiel #18
0
 /// <summary>
 /// Returns metadata about all of the versions of objects in a bucket.
 /// </summary>
 /// <param name="bucketName">A property of ListVersionsRequest used to execute the ListVersions service method.</param>
 /// <param name="prefix">Limits the response to keys that begin with the specified prefix.</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 ListVersions service method, as returned by S3.</returns>
 public Task<ListVersionsResponse> ListVersionsAsync(string bucketName, string prefix, System.Threading.CancellationToken cancellationToken = default(CancellationToken))
 {
     var request = new ListVersionsRequest();
     request.BucketName = bucketName;
     request.Prefix = prefix;
     return ListVersionsAsync(request, cancellationToken);
 }
Beispiel #19
0
        /// <summary>
        /// Deletes an S3 bucket which contains objects.
        /// An S3 bucket which contains objects cannot be deleted until all the objects 
        /// in it are deleted. The function deletes all the objects in the specified 
        /// bucket and then deletes the bucket itself.
        /// </summary>
        /// <param name="bucketName">The bucket to be deleted.</param>
        /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param>
        /// <param name="deleteOptions">Options to control the behavior of the delete operation.</param>
        /// <param name="updateCallback">The callback which is used to send updates about the delete operation.</param>
        /// <param name="asyncCancelableResult">An IAsyncCancelableResult that can be used to poll or wait for results, or both; 
        /// this value is also needed when invoking EndDeleteS3BucketWithObjects. IAsyncCancelableResult can also 
        /// be used to cancel the operation while it's in progress.</param>
        private static void DeleteS3BucketWithObjectsInternal(string bucketName, AmazonS3 s3Client,
            S3DeleteBucketWithObjectsOptions deleteOptions,Action<S3DeleteBucketWithObjectsUpdate> updateCallback,
            AsyncCancelableResult asyncCancelableResult)
        {
            // Validations.
            if (s3Client == null)
            {
                throw new ArgumentNullException("s3Client", "The s3Client cannot be null!");
            }

            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException("bucketName", "The bucketName cannot be null or empty string!");
            }

            var listVersionsRequest = new ListVersionsRequest { BucketName=bucketName };

            ListVersionsResponse listVersionsResponse;

            // Iterate through the objects in the bucket and delete them.
            do
            {
                // Check if the operation has been canceled.
                if (asyncCancelableResult.IsCancelRequested)
                {
                    // Signal that the operation is canceled.
                    asyncCancelableResult.SignalWaitHandleOnCanceled();
                    return;
                }

                // List all the versions of all the objects in the bucket.
                listVersionsResponse = s3Client.ListVersions(listVersionsRequest);

                if (listVersionsResponse.Versions.Count==0)
                {
                    // If the bucket has no objects break the loop.
                    break;
                }

                var keyVersionList = new List<KeyVersion>(listVersionsResponse.Versions.Count);
                for (int index = 0; index < listVersionsResponse.Versions.Count; index++)
                {
                    keyVersionList.Add(new KeyVersion(
                                                listVersionsResponse.Versions[index].Key,
                                                listVersionsResponse.Versions[index].VersionId
                                            ));
                }

                try
                {
                    // Delete the current set of objects.
                    var deleteObjectsResponse =
                        s3Client.DeleteObjects(new DeleteObjectsRequest
                        {
                            BucketName=bucketName,
                            Quiet=deleteOptions.QuietMode,
                            Keys=keyVersionList
                        });

                    if (!deleteOptions.QuietMode)
                    {
                        // If quiet mode is not set, update the client with list of deleted objects.
                        InvokeS3DeleteBucketWithObjectsUpdateCallback(
                                        updateCallback,
                                        new S3DeleteBucketWithObjectsUpdate
                                        {
                                            DeletedObjects = deleteObjectsResponse.DeletedObjects
                                        }
                                    );
                    }
                }
                catch (DeleteObjectsException deleteObjectsException)
                {
                    if (deleteOptions.ContinueOnError)
                    {
                        // Continue the delete operation if an error was encountered.
                        // Update the client with the list of objects that were deleted and the
                        // list of objects on which the delete failed.
                        InvokeS3DeleteBucketWithObjectsUpdateCallback(
                                updateCallback,
                                new S3DeleteBucketWithObjectsUpdate
                                {
                                    DeletedObjects = deleteObjectsException.ErrorResponse.DeletedObjects,
                                    DeleteErrors = deleteObjectsException.ErrorResponse.DeleteErrors
                                }
                            );
                    }
                    else
                    {
                        // Re-throw the exception if an error was encountered.
                        throw;
                    }
                }

                // Set the markers to get next set of objects from the bucket.
                listVersionsRequest.KeyMarker = listVersionsResponse.NextKeyMarker;
                listVersionsRequest.VersionIdMarker = listVersionsResponse.NextVersionIdMarker;

            }
            // Continue listing objects and deleting them until the bucket is empty.
            while (listVersionsResponse.IsTruncated);

            for (int attempts = 0; true; attempts++)
            {
                try
                {
                    // Bucket is empty, delete the bucket.
                    s3Client.DeleteBucket( new DeleteBucketRequest { BucketName = bucketName } );

                    break;
                }
                catch (AmazonS3Exception e)
                {
                    if (!string.Equals(e.ErrorCode, S3Constants.BucketNotEmpty) || attempts >= 3)
                        throw;
                    Thread.Sleep(5 * 1000);
                }
            }

            // Signal that the operation is completed.
            asyncCancelableResult.SignalWaitHandleOnCompleted();
        }
        /// <summary>
        /// Initiates the asynchronous execution of the ListVersions operation.
        /// </summary>
        /// 
        /// <param name="request">Container for the necessary parameters to execute the ListVersions 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 EndListVersions
        ///         operation.</returns>
        public IAsyncResult BeginListVersions(ListVersionsRequest request, AsyncCallback callback, object state)
        {
            var marshaller = new ListVersionsRequestMarshaller();
            var unmarshaller = ListVersionsResponseUnmarshaller.Instance;

            return BeginInvoke<ListVersionsRequest>(request, marshaller, unmarshaller,
                callback, state);
        }
Beispiel #21
0
 /// <summary>
 /// Returns metadata about all of the versions of objects in a bucket.
 /// </summary>
 /// <param name="bucketName">A property of ListVersionsRequest used to execute the ListVersions service method.</param>
 /// <param name="prefix">Limits the response to keys that begin with the specified prefix.</param>
 /// 
 /// <returns>The response from the ListVersions service method, as returned by S3.</returns>
 public ListVersionsResponse ListVersions(string bucketName, string prefix)
 {
     var request = new ListVersionsRequest();
     request.BucketName = bucketName;
     request.Prefix = prefix;
     return ListVersions(request);
 }
Beispiel #22
0
        private IAsyncEnumerable<S3ObjectVersion> ListObjects(string prefix)
        {
            return AsyncEnumerableEx.Create<S3ObjectVersion>(async y =>
            {
                var request = new ListVersionsRequest
                {
                    BucketName = _bucket,
                    Prefix = prefix,
                    KeyMarker = null,
                    VersionIdMarker = null
                };

                while (request != null && !y.CancellationToken.IsCancellationRequested)
                {
                    var resp = await _client.ListVersionsAsync(request, y.CancellationToken).ConfigureAwait(false);
                    foreach (var v in resp.Versions)
                    {
                        await y.YieldReturn(v).ConfigureAwait(false);
                    }

                    if (resp.IsTruncated)
                    {
                        request.KeyMarker = resp.NextKeyMarker;
                        request.VersionIdMarker = resp.NextVersionIdMarker;
                    }
                    else
                    {
                        request = null;
                    }
                }
            });
        }
Beispiel #23
0
        /// <summary>
        /// Initiates the asynchronous execution of the ListVersions operation.
        /// <seealso cref="Amazon.S3.IAmazonS3.ListVersions"/>
        /// </summary>
        /// 
        /// <param name="request">Container for the necessary parameters to execute the ListVersions 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<ListVersionsResponse> ListVersionsAsync(ListVersionsRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            var marshaller = new ListVersionsRequestMarshaller();
            var unmarshaller = ListVersionsResponseUnmarshaller.GetInstance();
            return Invoke<IRequest, ListVersionsRequest, ListVersionsResponse>(request, marshaller, unmarshaller, signer, cancellationToken);
        }
Beispiel #24
0
        /// <summary>
        /// <para>Returns metadata about all of the versions of objects in a bucket.</para>
        /// </summary>
        /// 
        /// <param name="request">Container for the necessary parameters to execute the ListVersions service method on
        /// AmazonS3.</param>
        /// 
        /// <returns>The response from the ListVersions service method, as returned by AmazonS3.</returns>
		public ListVersionsResponse ListVersions(ListVersionsRequest request)
        {
            var task = ListVersionsAsync(request);
            try
            {
                return task.Result;
            }
            catch(AggregateException e)
            {
                throw e.InnerException;
            }
        }
        internal ListVersionsResponse ListVersions(ListVersionsRequest request)
        {
            var marshaller = new ListVersionsRequestMarshaller();
            var unmarshaller = ListVersionsResponseUnmarshaller.Instance;

            return Invoke<ListVersionsRequest,ListVersionsResponse>(request, marshaller, unmarshaller);
        }
Beispiel #26
0
        public static void DeleteBucketWithObjects(IAmazonS3 s3Client, string bucketName)
        {
            // Validations.
            if (s3Client == null)
            {
                throw new ArgumentNullException("s3Client", "The s3Client cannot be null!");
            }

            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException("bucketName", "The bucketName cannot be null or empty string!");
            }

            var listVersionsRequest = new ListVersionsRequest
            {
                BucketName = bucketName
            };

            ListVersionsResponse listVersionsResponse = null;
            string lastRequestId = null;

            var exception = new Exception();
            var mre = new AutoResetEvent(false);

            // Iterate through the objects in the bucket and delete them.
            do
            {
                // List all the versions of all the objects in the bucket.
                s3Client.ListVersionsAsync(listVersionsRequest, (result) =>
               {
                   exception = result.Exception;
                   listVersionsResponse = result.Response;
                   mre.Set();
               }, new AsyncOptions() { ExecuteCallbackOnMainThread = false });
                mre.WaitOne();
                Utils.AssertExceptionIsNull(exception);


                lastRequestId = listVersionsResponse.ResponseMetadata.RequestId;

                if (listVersionsResponse.Versions.Count == 0)
                {
                    // If the bucket has no objects break the loop.
                    break;
                }

                var keyVersionList = new List<KeyVersion>(listVersionsResponse.Versions.Count);
                for (int index = 0; index < listVersionsResponse.Versions.Count; index++)
                {
                    keyVersionList.Add(new KeyVersion
                    {
                        Key = listVersionsResponse.Versions[index].Key,
                        VersionId = listVersionsResponse.Versions[index].VersionId
                    });
                }


                var deleteObjectsResponse = new DeleteObjectsResponse();
                // Delete the current set of objects.
                s3Client.DeleteObjectsAsync(new DeleteObjectsRequest
                {
                    BucketName = bucketName,
                    Objects = keyVersionList,
                    Quiet = true
                }, (result) =>
                {
                    deleteObjectsResponse = result.Response;
                    exception = result.Exception;
                    mre.Set();
                }, new AsyncOptions() { ExecuteCallbackOnMainThread = false });
                mre.WaitOne();
                Utils.AssertExceptionIsNull(exception);

                // Set the markers to get next set of objects from the bucket.
                listVersionsRequest.KeyMarker = listVersionsResponse.NextKeyMarker;
                listVersionsRequest.VersionIdMarker = listVersionsResponse.NextVersionIdMarker;

            }
            // Continue listing objects and deleting them until the bucket is empty.
            while (listVersionsResponse.IsTruncated);

            // Bucket is empty, delete the bucket.
            s3Client.DeleteBucketAsync(new DeleteBucketRequest
            {
                BucketName = bucketName
            }, (result) =>
            {
                exception = result.Exception;
                mre.Set();
            }, new AsyncOptions() { ExecuteCallbackOnMainThread = false });

            mre.WaitOne();
            Utils.AssertExceptionIsNull(exception);
        }
        /// <summary>
        /// Deletes an S3 bucket which contains objects.
        /// An S3 bucket which contains objects cannot be deleted until all the objects 
        /// in it are deleted. The function deletes all the objects in the specified 
        /// bucket and then deletes the bucket itself.
        /// </summary>
        /// <param name="bucketName">The bucket to be deleted.</param>
        /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param>
        /// <param name="deleteOptions">Options to control the behavior of the delete operation.</param>
        /// <param name="updateCallback">The callback which is used to send updates about the delete operation.</param>
        /// <param name="token">token to check if the operation has been request to cancel.</param>
        private static async Task DeleteS3BucketWithObjectsInternalAsync(IAmazonS3 s3Client, string bucketName,
            S3DeleteBucketWithObjectsOptions deleteOptions, Action<S3DeleteBucketWithObjectsUpdate> updateCallback,
            CancellationToken token)
        {
            // Validations.
            if (s3Client == null)
            {
                throw new ArgumentNullException("s3Client", "The s3Client cannot be null!");
            }

            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException("bucketName", "The bucketName cannot be null or empty string!");
            }

            var listVersionsRequest = new ListVersionsRequest
            {
                BucketName = bucketName
            };

            ListVersionsResponse listVersionsResponse;

            // Iterate through the objects in the bucket and delete them.
            do
            {
                // Check if the operation has been canceled.
                if (token.IsCancellationRequested)
                {
                    // Signal that the operation is canceled.
                    return;
                }

                // List all the versions of all the objects in the bucket.
                listVersionsResponse = await s3Client.ListVersionsAsync(listVersionsRequest,token).ConfigureAwait(false);

                if (listVersionsResponse.Versions.Count == 0)
                {
                    // If the bucket has no objects break the loop.
                    break;
                }

                var keyVersionList = new List<KeyVersion>(listVersionsResponse.Versions.Count);
                for (int index = 0; index < listVersionsResponse.Versions.Count; index++)
                {
                    keyVersionList.Add(new KeyVersion
                    {
                        Key = listVersionsResponse.Versions[index].Key,
                        VersionId = listVersionsResponse.Versions[index].VersionId
                    });
                }

                try
                {
                    // Delete the current set of objects.
                    var deleteObjectsResponse = await s3Client.DeleteObjectsAsync(new DeleteObjectsRequest
                    {
                        BucketName = bucketName,
                        Objects = keyVersionList,
                        Quiet = deleteOptions.QuietMode
                    },token).ConfigureAwait(false);

                    if (!deleteOptions.QuietMode)
                    {
                        // If quiet mode is not set, update the client with list of deleted objects.
                        InvokeS3DeleteBucketWithObjectsUpdateCallback(
                                        updateCallback,
                                        new S3DeleteBucketWithObjectsUpdate
                                        {
                                            DeletedObjects = deleteObjectsResponse.DeletedObjects
                                        }
                                    );
                    }
                }
                catch (DeleteObjectsException deleteObjectsException)
                {
                    if (deleteOptions.ContinueOnError)
                    {
                        // Continue the delete operation if an error was encountered.
                        // Update the client with the list of objects that were deleted and the 
                        // list of objects on which the delete failed.
                        InvokeS3DeleteBucketWithObjectsUpdateCallback(
                                updateCallback,
                                new S3DeleteBucketWithObjectsUpdate
                                {
                                    DeletedObjects = deleteObjectsException.Response.DeletedObjects,
                                    DeleteErrors = deleteObjectsException.Response.DeleteErrors
                                }
                            );
                    }
                    else
                    {
                        // Re-throw the exception if an error was encountered.
                        throw;
                    }
                }

                // Set the markers to get next set of objects from the bucket.
                listVersionsRequest.KeyMarker = listVersionsResponse.NextKeyMarker;
                listVersionsRequest.VersionIdMarker = listVersionsResponse.NextVersionIdMarker;

            }
            // Continue listing objects and deleting them until the bucket is empty.
            while (listVersionsResponse.IsTruncated);

            const int maxRetries = 10;
            for (int retries = 1; retries <= maxRetries; retries++)
            {
                try
                {
                    // Bucket is empty, delete the bucket.
                    await s3Client.DeleteBucketAsync(new DeleteBucketRequest
                    {
                        BucketName = bucketName
                    },token).ConfigureAwait(false);
                    break;
                }
                catch (AmazonS3Exception e)
                {
                    if (e.StatusCode != HttpStatusCode.Conflict || retries == maxRetries)
                        throw;
                    else
                        DefaultRetryPolicy.WaitBeforeRetry(retries, 5000);
                }
            }
        }
Beispiel #28
-1
        public void ObjectSamples()
        {
            {
                #region ListObjects Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // List all objects
                ListObjectsRequest listRequest = new ListObjectsRequest
                {
                    BucketName = "SampleBucket",
                };

                ListObjectsResponse listResponse;
                do
                {
                    // Get a list of objects
                    listResponse = client.ListObjects(listRequest);
                    foreach (S3Object obj in listResponse.S3Objects)
                    {
                        Console.WriteLine("Object - " + obj.Key);
                        Console.WriteLine(" Size - " + obj.Size);
                        Console.WriteLine(" LastModified - " + obj.LastModified);
                        Console.WriteLine(" Storage class - " + obj.StorageClass);
                    }

                    // Set the marker property
                    listRequest.Marker = listResponse.NextMarker;
                } while (listResponse.IsTruncated);

                #endregion
            }

            {
                #region GetObject Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a GetObject request
                GetObjectRequest request = new GetObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1"
                };

                // Issue request and remember to dispose of the response
                using (GetObjectResponse response = client.GetObject(request))
                {
                    using (StreamReader reader = new StreamReader(response.ResponseStream))
                    {
                        string contents = reader.ReadToEnd();
                        Console.WriteLine("Object - " + response.Key);
                        Console.WriteLine(" Version Id - " + response.VersionId);
                        Console.WriteLine(" Contents - " + contents);
                    }
                }

                #endregion
            }

            {
                #region GetObjectMetadata Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();


                // Create a GetObjectMetadata request
                GetObjectMetadataRequest request = new GetObjectMetadataRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1"
                };

                // Issue request and view the response
                GetObjectMetadataResponse response = client.GetObjectMetadata(request);
                Console.WriteLine("Content Length - " + response.ContentLength);
                Console.WriteLine("Content Type - " + response.Headers.ContentType);
                if (response.Expiration != null)
                {
                    Console.WriteLine("Expiration Date - " + response.Expiration.ExpiryDate);
                    Console.WriteLine("Expiration Rule Id - " + response.Expiration.RuleId);
                }

                #endregion
            }

            {
                #region PutObject Sample 1

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a PutObject request
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    ContentBody = "This is sample content..."
                };

                // Put object
                PutObjectResponse response = client.PutObject(request);

                #endregion
            }

            {
                #region PutObject Sample 2

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a PutObject request
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    FilePath = "contents.txt"
                };

                // Put object
                PutObjectResponse response = client.PutObject(request);

                #endregion
            }

            {
                #region PutObject Sample 3

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a PutObject request
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                };
                using (FileStream stream = new FileStream("contents.txt", FileMode.Open))
                {
                    request.InputStream = stream;

                    // Put object
                    PutObjectResponse response = client.PutObject(request);
                }

                #endregion
            }

            {
                #region DeleteObject Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a DeleteObject request
                DeleteObjectRequest request = new DeleteObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1"
                };

                // Issue request
                client.DeleteObject(request);

                #endregion
            }

            {
                #region DeleteObjects Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a DeleteObject request
                DeleteObjectsRequest request = new DeleteObjectsRequest
                {
                    BucketName = "SampleBucket",
                    Objects = new List<KeyVersion>
                    {
                        new KeyVersion() {Key = "Item1"},
                        // Versioned item
                        new KeyVersion() { Key = "Item2", VersionId = "Rej8CiBxcZKVK81cLr39j27Y5FVXghDK", },
                        // Item in subdirectory
                        new KeyVersion() { Key = "Logs/error.txt"}
                    }
                };

                try
                {
                    // Issue request
                    DeleteObjectsResponse response = client.DeleteObjects(request);
                }
                catch (DeleteObjectsException doe)
                {
                    // Catch error and list error details
                    DeleteObjectsResponse errorResponse = doe.Response;

                    foreach (DeletedObject deletedObject in errorResponse.DeletedObjects)
                    {
                        Console.WriteLine("Deleted item " + deletedObject.Key);
                    }
                    foreach (DeleteError deleteError in errorResponse.DeleteErrors)
                    {
                        Console.WriteLine("Error deleting item " + deleteError.Key);
                        Console.WriteLine(" Code - " + deleteError.Code);
                        Console.WriteLine(" Message - " + deleteError.Message);
                    }
                }

                #endregion
            }

            {
                #region CopyObject Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a CopyObject request
                CopyObjectRequest request = new CopyObjectRequest
                {
                    SourceBucket = "SampleBucket",
                    SourceKey = "Item1",
                    DestinationBucket = "AnotherBucket",
                    DestinationKey = "Copy1",
                    CannedACL = S3CannedACL.PublicRead
                };

                // Issue request
                client.CopyObject(request);

                #endregion
            }

            {
                #region CopyObject Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a CopyObject request
                CopyObjectRequest request = new CopyObjectRequest
                {
                    SourceBucket = "SampleBucket",
                    SourceKey = "Item1",
                    DestinationBucket = "AnotherBucket",
                    DestinationKey = "Copy1",
                    CannedACL = S3CannedACL.PublicRead
                };

                // Issue request
                client.CopyObject(request);

                #endregion
            }

            {
                #region ListVersions Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Turn versioning on for a bucket
                client.PutBucketVersioning(new PutBucketVersioningRequest
                {
                    BucketName = "SampleBucket",
                    VersioningConfig = new S3BucketVersioningConfig { Status = "Enable" }
                });

                // Populate bucket with multiple items, each with multiple versions
                PopulateBucket(client, "SampleBucket");

                // Get versions
                ListVersionsRequest request = new ListVersionsRequest
                {
                    BucketName = "SampleBucket"
                };

                // Make paged ListVersions calls
                ListVersionsResponse response;
                do
                {
                    response = client.ListVersions(request);
                    // View information about versions
                    foreach (var version in response.Versions)
                    {
                        Console.WriteLine("Key = {0}, Version = {1}, IsLatest = {2}, LastModified = {3}, Size = {4}",
                            version.Key,
                            version.VersionId,
                            version.IsLatest,
                            version.LastModified,
                            version.Size);
                    }

                    request.KeyMarker = response.NextKeyMarker;
                    request.VersionIdMarker = response.NextVersionIdMarker;
                } while (response.IsTruncated);

                #endregion
            }

            {
                #region Multipart Upload Sample

                int MB = (int)Math.Pow(2, 20);

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Define input stream
                Stream inputStream = Create13MBDataStream();

                // Initiate multipart upload
                InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1"
                };
                InitiateMultipartUploadResponse initResponse = client.InitiateMultipartUpload(initRequest);

                // Upload part 1
                UploadPartRequest uploadRequest = new UploadPartRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId,
                    PartNumber = 1,
                    PartSize = 5 * MB,
                    InputStream = inputStream
                };
                UploadPartResponse up1Response = client.UploadPart(uploadRequest);

                // Upload part 2
                uploadRequest = new UploadPartRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId,
                    PartNumber = 2,
                    PartSize = 5 * MB,
                    InputStream = inputStream
                };
                UploadPartResponse up2Response = client.UploadPart(uploadRequest);

                // Upload part 3
                uploadRequest = new UploadPartRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId,
                    PartNumber = 3,
                    InputStream = inputStream
                };
                UploadPartResponse up3Response = client.UploadPart(uploadRequest);

                // List parts for current upload
                ListPartsRequest listPartRequest = new ListPartsRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId
                };
                ListPartsResponse listPartResponse = client.ListParts(listPartRequest);
                Debug.Assert(listPartResponse.Parts.Count == 3);

                // Complete the multipart upload
                CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId,
                    PartETags = new List<PartETag>
                    {
                        new PartETag { ETag = up1Response.ETag, PartNumber = 1 },
                        new PartETag { ETag = up2Response.ETag, PartNumber = 2 },
                        new PartETag { ETag = up3Response.ETag, PartNumber = 3 }
                    }
                };
                CompleteMultipartUploadResponse compResponse = client.CompleteMultipartUpload(compRequest);

                #endregion
            }
        }