public void DeleteBucket(string bucket_name, AWS_Credentials credentials) { if (credentials is null || credentials.AWS_AccessKey is null || credentials.AWS_SecretKey is null || credentials.Region is null) { throw new CredentialsNotProvidedException(); } if (bucket_name is null) { throw new ArgumentNullException(); } using (AmazonS3Client client = getS3Client(credentials)) { List <string> existing_buckets = GetAllBuckets(credentials); try { if (!(existing_buckets.Where(b => b.ToString() == bucket_name).FirstOrDefault() is null)) { var request = new DeleteBucketRequest { BucketName = bucket_name, UseClientRegion = true }; Task <DeleteBucketResponse> task = client.DeleteBucketAsync(request); task.Wait(); } } catch (Exception e) { throw e; } } }
public static async Task DeleteBucket(string bucketName) { using (var client = new AmazonS3Client(ConfigManager.ConfigSettings.AccessKey, ConfigManager.ConfigSettings.Secret, Amazon.RegionEndpoint.USWest2)) { await client.DeleteBucketAsync(bucketName); } }
/// <summary> /// Method deletes bucket from the S3 /// </summary> /// <param name="bucketName">Name of the bucket</param> public void DeleteBucket(string bucketName) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } DeleteBucketRequest s3DeleteBucketRequest = new DeleteBucketRequest(); s3DeleteBucketRequest.BucketName = bucketName; s3DeleteBucketRequest.UseClientRegion = true; try { Task <DeleteBucketResponse> s3DeleteBucketResponse = _client.DeleteBucketAsync(s3DeleteBucketRequest); Console.WriteLine($"HTTP status code : {s3DeleteBucketResponse.Result.HttpStatusCode}"); } catch (System.AggregateException ex) { if (ex.InnerException != null) { Console.WriteLine($"Message: {ex.InnerException.Message}"); } } }
private async Task DeleteDeploymentZipAndBucketAsync(AmazonS3Client s3Client, string bucketName) { // Delete the deployment zip. // This is idempotent - it works even if the object is not there. var deleteObjectRequest = new DeleteObjectRequest { BucketName = bucketName, Key = DeploymentZipKey }; await s3Client.DeleteObjectAsync(deleteObjectRequest); // Delete the bucket. // Make idempotent by checking exception. var deleteBucketRequest = new DeleteBucketRequest { BucketName = bucketName }; try { await s3Client.DeleteBucketAsync(deleteBucketRequest); } catch (AmazonS3Exception e) { // If it's just telling us the bucket's not there then continue, otherwise throw. if (!e.Message.Contains("The specified bucket does not exist")) { throw; } } }
private static async Task DeleteBucketAsync() { var config = new AmazonS3Config { RegionEndpoint = RegionEndpoint.USEast1, // MUST set this before setting ServiceURL and it should match the `MINIO_REGION` environment variable. ServiceURL = endpointURL, // replace http://localhost:9000 with URL of your MinIO server ForcePathStyle = true // MUST be true to work correctly with MinIO server }; var amazonS3Client = new AmazonS3Client(accessKey, secretKey, config); try { var deleteBucketRequest = new DeleteBucketRequest { BucketName = bucketName, UseClientRegion = true }; DeleteBucketResponse deleteBucketResponse = await amazonS3Client.DeleteBucketAsync(bucketName); Console.Out.WriteLine("Status code = '" + deleteBucketResponse.HttpStatusCode); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } }
public async Task DeleteBucket(String bucketName) { DeleteBucketResponse response = null; DeleteBucketRequest request = new DeleteBucketRequest { BucketName = bucketName }; bool found = await IfBucketExist(bucketName); if (!found) { Console.WriteLine("bucket not found"); } else { try { response = await client.DeleteBucketAsync(request); if (response.HttpStatusCode.ToString() == "OK") { Console.WriteLine("Bucket " + bucketName + " was deleted"); } } catch (AmazonS3Exception e) { Console.WriteLine("Bucket is not empty.. Emptying bucket"); Console.WriteLine(e.ToString()); } } }
public void DeleteBucket(string bucket_name, AmazonS3Client client) { if (bucket_name is null) { throw new ArgumentNullException(); } List <string> existing_buckets = GetAllBuckets(client); try { if (!(existing_buckets.Where(b => b.ToString() == bucket_name).FirstOrDefault() is null)) { var request = new DeleteBucketRequest { BucketName = bucket_name, UseClientRegion = true }; Task <DeleteBucketResponse> task = client.DeleteBucketAsync(request); task.Wait(); } } catch (Exception e) { throw e; } }
protected override async Task DoDeleteAllAsync(CancellationToken?cancellationToken = null) { if (await IsBucketExistsAsync(Options.Bucket)) { if (string.IsNullOrEmpty(Options.Prefix)) { try { await AmazonS3Util.DeleteS3BucketWithObjectsAsync(_s3Client, Options.Bucket, cancellationToken ?? CancellationToken.None); } catch (AmazonS3Exception ex) when(ex.Message.Contains( "A header you provided implies functionality that is not implemented")) { await DeleteAllObjectsInBucket(cancellationToken); await _s3Client.DeleteBucketAsync(Options.Bucket); } } else { await DeleteAllObjectsInBucket(cancellationToken); } } }
public static async Task DeleteAllBuckets() { using (var client = new AmazonS3Client(ConfigManager.ConfigSettings.AccessKey, ConfigManager.ConfigSettings.Secret, Amazon.RegionEndpoint.USWest2)) { var response = await client.ListBucketsAsync(); foreach (var bucket in response.Buckets) { await DeleteAllBucketItems(bucket.BucketName); await client.DeleteBucketAsync(bucket.BucketName); } } }
public static void DeleteBucket(AmazonS3Client s3, string bucketName) { DeleteBucketRequest req = new DeleteBucketRequest { BucketName = bucketName }; Task <DeleteBucketResponse> res = s3.DeleteBucketAsync(req); Task.WaitAll(res); if (res.IsCompletedSuccessfully) { Console.WriteLine("Deleted bucket - '{0}'", bucketName); } }
public async Task DeleteService(string name, CPSAuthModel auth) { try { AmazonCloudFormationClient cloudFormationClient = new AmazonCloudFormationClient(auth.AccessId, auth.AccessSecret, Amazon.RegionEndpoint.GetBySystemName(auth.AccessRegion)); var responseCloudFormationClient = await cloudFormationClient.DeleteStackAsync(new DeleteStackRequest() { StackName = name.ToLower() }); string bucketName = name.ToLower(); AmazonS3Client s3Client = new AmazonS3Client(auth.AccessId, auth.AccessSecret, Amazon.RegionEndpoint.GetBySystemName(auth.AccessRegion)); ListObjectsResponse response = await s3Client.ListObjectsAsync(new ListObjectsRequest() { BucketName = bucketName }); if (response.S3Objects.Count > 0) { List <KeyVersion> keys = response.S3Objects.Select(obj => new KeyVersion() { Key = obj.Key }).ToList(); DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keys }; await s3Client.DeleteObjectsAsync(deleteObjectsRequest); } //Delete Bucket DeleteBucketRequest request = new DeleteBucketRequest { BucketName = bucketName }; await s3Client.DeleteBucketAsync(request); } catch (Exception ex) { TelemetryClientManager.Instance.TrackException(ex); Console.WriteLine(ex.ToString()); } }
public async Task <bool> DeleteBucketAsync(string bucketName) { DeleteBucketRequest request = new DeleteBucketRequest { BucketName = bucketName.ToLower(), }; try { DeleteBucketResponse response = await _amazonS3Client.DeleteBucketAsync(request); return((response.HttpStatusCode == HttpStatusCode.NoContent) ? true : false); } catch (Exception e) { _logger.LogError(e.Message); throw; } }
public static async Task <bool> DeleBucket(AmazonS3Client amazonS3Client, ListBucketsResponse listBucketsResponse) { bool isDel = true; foreach (S3Bucket bucket in listBucketsResponse.Buckets) { ListObjectsResponse getObjectResponse = await amazonS3Client.ListObjectsAsync(bucket.BucketName); if (!await DeleObject(amazonS3Client, getObjectResponse)) { isDel = false; } DeleteBucketResponse deleteBucketResponse = await amazonS3Client.DeleteBucketAsync(bucket.BucketName); if (deleteBucketResponse.HttpStatusCode != HttpStatusCode.NoContent) { Console.WriteLine($"删除失败 {Environment.NewLine}BucketName:{bucket.BucketName}BucketName:{bucket.CreationDate}"); throw new Exception("删除Bucket失败"); } } return(isDel); }
private void DestroyBucket() { using (var amazonS3Client = new AmazonS3Client(_amazonS3Config)) { var listObjectsResponse = amazonS3Client.ListObjectsV2Async(new ListObjectsV2Request { BucketName = _bucketName }).Result; if (listObjectsResponse.S3Objects.Any()) { amazonS3Client.DeleteObjectsAsync(new DeleteObjectsRequest { BucketName = _bucketName, Objects = listObjectsResponse.S3Objects.Select(x => new KeyVersion { Key = x.Key }).ToList() }).Wait(); } amazonS3Client.DeleteBucketAsync(_bucketName).Wait(); } }
//[Fact] //public async Task ContainersAreRemovedOnShutdown() //{ // int rnd = new Random().Next(10000, 12000 - 1); // async Task<string> StartStopContainer() // { // var fixture = new S3Fixture(); // var options = new S3FixtureOptionsWithOwnHost(); // // This ensures that the container will be unique and not being used by a diff test. // fixture.Ports[0] = rnd; // await fixture.Start(options); // Assert.True(fixture.ContainerStarting); // Assert.True(fixture.ContainerStarted); // string id = fixture.ContainerId; // await Task.Delay(1000); // fixture.Dispose(); // await Task.Delay(1000); // options.ContainerHost.Dispose(); // await Task.Delay(1000); // return id; // } // var id1 = await StartStopContainer(); // await Task.Delay(5000); // var id2 = await StartStopContainer(); // Assert.NotEqual(id1, id2); //} private async Task <S3Fixture> hitS3(S3Fixture fixture) { await fixture.Start(); var s3Client = new AmazonS3Client( new AnonymousAWSCredentials(), new AmazonS3Config { ServiceURL = "http://127.0.0.1:" + fixture.Ports.Single(), ForcePathStyle = true, Timeout = TimeSpan.FromSeconds(5) }); var cts = new CancellationTokenSource(); cts.CancelAfter(6000); const string bucketName = "bar"; await s3Client.PutBucketAsync(new PutBucketRequest { BucketName = bucketName }, cts.Token); await s3Client.ListBucketsAsync(cts.Token); await s3Client.DeleteBucketAsync(bucketName, cts.Token); return(fixture); }
private AmazonWebServiceResponse SubmitRequest() { switch (m_MethodInfo.Operation) { case S3Operation.GetObject: GetObjectRequest gorequest = CreateGetObjectRequest(); return(m_AmazonS3.GetObjectAsync(gorequest).GetAwaiter().GetResult()); case S3Operation.ListObject: ListObjectsRequest lorequest = CreateListObjectsRequest(); return(m_AmazonS3.ListObjectsAsync(lorequest).GetAwaiter().GetResult()); case S3Operation.ListBuckets: ListBucketsRequest lbrequest = CreateListBucketsRequest(); return(m_AmazonS3.ListBucketsAsync(lbrequest).GetAwaiter().GetResult()); case S3Operation.PutBucket: PutBucketRequest pbrequest = CreatePutBucketsRequest(); return(m_AmazonS3.PutBucketAsync(pbrequest).GetAwaiter().GetResult()); case S3Operation.PutObject: PutObjectRequest porequest = CreatePutObjectRequest(); return(m_AmazonS3.PutObjectAsync(porequest).GetAwaiter().GetResult()); case S3Operation.DeleteObject: DeleteObjectRequest dorequest = CreateDeleteObjectRequest(); return(m_AmazonS3.DeleteObjectAsync(dorequest).GetAwaiter().GetResult()); case S3Operation.RemoveBucket: DeleteBucketRequest dbrequest = CreateDeleteBucketRequest(); return(m_AmazonS3.DeleteBucketAsync(dbrequest).GetAwaiter().GetResult()); default: throw new NotSupportedException("S3 operation " + m_MethodInfo.Operation + " is not supported"); } }
public async Task ImageProcessor_CanResizeImage() { var s3Client = new AmazonS3Client(RegionEndpoint.USEast1); var bucketName = $"hs-processor-test-{DateTime.Now.Ticks}"; await s3Client.PutBucketAsync(bucketName); try { // TODO: Specify a test file path (on your local machine) var testFile = new FileInfo(@""); string testKey = $"uploaded/{DateTime.Now.Year}/{DateTime.Now.Month}/test.jpg"; var putObjectRequest = new PutObjectRequest { Key = testKey, BucketName = bucketName, InputStream = testFile.OpenRead(), ContentType = "image/jpeg" }; await s3Client.PutObjectAsync(putObjectRequest); var s3Event = new S3Event { Records = new List <S3EventNotification.S3EventNotificationRecord> { new S3EventNotification.S3EventNotificationRecord { S3 = new S3EventNotification.S3Entity { Bucket = new S3EventNotification.S3BucketEntity { Name = bucketName }, Object = new S3EventNotification.S3ObjectEntity { Key = testKey } } } } }; var testFunction = new ImageHandler(s3Client); var result = await testFunction.ProcessImage(s3Event, new TestLambdaContext()); Assert.Equal("Ok", result); } finally { // Delete all the test objects so the bucket can be removed var bucketObjects = await s3Client.ListObjectsAsync(bucketName); foreach (var s3obj in bucketObjects.S3Objects) { await s3Client.DeleteObjectAsync(bucketName, s3obj.Key); } var deleteResponse = await s3Client.DeleteBucketAsync(bucketName); Console.WriteLine("Delete bucket response " + deleteResponse.HttpStatusCode); } }
public async Task DeleteBucket(string name = null) { string bucketName = name; if (bucketName == null) { bucketName = awsS3Options.Bucket; } using (AmazonS3Client client = CreateClient()) { var exists = await AmazonS3Util.DoesS3BucketExistAsync(client, bucketName); if (exists) { // make all objects expire so as to empty the bucket //await client.PutLifecycleConfigurationAsync(new PutLifecycleConfigurationRequest() //{ // BucketName = bucketName, // Configuration = new LifecycleConfiguration() // { // Rules = { // new LifecycleRule() // { // Expiration = new LifecycleRuleExpiration() // { // Date = DateTime.Now.AddDays(-10) // }, // NoncurrentVersionExpiration = new LifecycleRuleNoncurrentVersionExpiration() // { // NoncurrentDays = 0 // } // } // } // } //}); // may need to iterate the objects for deleting //string continuationToken = null; //do //{ // var response = await client.ListObjectsV2Async(new ListObjectsV2Request() // { // BucketName = bucketName, // ContinuationToken = continuationToken, // }); // continuationToken = response.ContinuationToken; // foreach(var s3Object in response.S3Objects) // { // var deleteResponse = await client.DeleteObjectAsync(bucketName, s3Object.Key); // } //} //while (continuationToken != null); await client.DeleteBucketAsync(new DeleteBucketRequest() { BucketName = bucketName, BucketRegion = awsS3Options.Region }); } } }
/// <summary> /// Delete a bucket. /// </summary> /// <param name="bucketName">The bucket name.</param> /// <returns>The async task.</returns> public async Task <DeleteBucketResponse> DeleteBucketAsync(string bucketName) { return(await _client.DeleteBucketAsync(bucketName)); }
static void DeleteBucket(AmazonS3Client s3) { DeleteBucketRequest req = new DeleteBucketRequest(); req.BucketName = bucketName; Task <DeleteBucketResponse> res = s3.DeleteBucketAsync(req); Task.WaitAll(res); if (res.IsCompletedSuccessfully) { { Console.WriteLine("Deleted bucket - '{0}'", bucketName); } } }
protected async void Button1_Click(object sender, EventArgs e) { // define the Bucket, DWG and PDF file names string bucketName = "designautomationsample-" + Guid.NewGuid().ToString(); string dwgFileName = FileUpload1.FileName; string pdfFileName = dwgFileName.Replace(".dwg", ".pdf"); // upload from client/browser to my server string fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), dwgFileName); Directory.CreateDirectory(Path.GetDirectoryName(fileSavePath)); FileUpload1.SaveAs(fileSavePath); IAmazonS3 client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2); // create AWS Bucket if (!await client.DoesS3BucketExistAsync(bucketName)) { await client.EnsureBucketExistsAsync(bucketName); } // Upload file from Server to S3 bucket client.UploadObjectFromFilePath(bucketName, FileUpload1.FileName, fileSavePath, null); // delete files from server Directory.Delete(Path.GetDirectoryName(fileSavePath), true); // OAuht 2-legged on Forge TwoLeggedApi apiInstance = new TwoLeggedApi(); dynamic bearer = await apiInstance.AuthenticateAsync(Config.FORGE_CLIENT_ID, Config.FORGE_CLIENT_SECRET, Autodesk.Forge.oAuthConstants.CLIENT_CREDENTIALS, Config.FORGE_SCOPE_DESIGN_AUTOMATION); // generate URLs for Design Automation to access (download & upload) S3 files Uri downloadFromS3 = new Uri(client.GeneratePreSignedURL(bucketName, dwgFileName, DateTime.Now.AddMinutes(90), null)); Dictionary <string, object> props = new Dictionary <string, object>(); props.Add("Verb", "PUT"); Uri uploadToS3 = new Uri(client.GeneratePreSignedURL(bucketName, pdfFileName, DateTime.Now.AddMinutes(10), props)); // prepare WorkItem (based on the built-in "PlotToPDF" activity") WorkItemsApi workItemsApi = new WorkItemsApi(); workItemsApi.Configuration.AccessToken = bearer.access_token; JObject arguments = new JObject { new JProperty( "InputArguments", new JArray { new JObject { new JProperty("Resource", downloadFromS3.GetLeftPart(UriPartial.Path)), new JProperty("Headers", MakeHeaders(WebRequestMethods.Http.Get, downloadFromS3)), new JProperty("Name", "HostDwg") } } ), new JProperty( "OutputArguments", new JArray { new JObject { new JProperty("Name", "Result"), new JProperty("HttpVerb", "PUT"), new JProperty("Resource", uploadToS3.GetLeftPart(UriPartial.Path)), new JProperty("Headers", MakeHeaders(WebRequestMethods.Http.Put, uploadToS3)), new JProperty("StorageProvider", "Generic") } } ) }; // submit the workitem... dynamic workitem = await workItemsApi.CreateWorkItemAsync(new Autodesk.Forge.Model.WorkItem(string.Empty, arguments, null, null, null, "PlotToPDF")); // wait... System.Threading.Thread.Sleep(5000); // wait 1 second // get the status string id = workitem.Id; dynamic status = await workItemsApi.GetWorkItemAsync(id); // Due an issue with the .NET SDK, this is not working (#17) // download the PDF from S3 to our server fileSavePath = fileSavePath.Replace(".dwg", ".pdf"); client.DownloadToFilePath(bucketName, pdfFileName, fileSavePath, null); // send the PDF file to the client (DO NOT expose a direct URL to S3, but send the bytes) Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment;filename=\"" + pdfFileName + "\""); Response.BinaryWrite(File.ReadAllBytes(fileSavePath)); Response.Flush(); Response.End(); // delete files on S3 await client.DeleteObjectAsync(bucketName, dwgFileName); await client.DeleteObjectAsync(bucketName, pdfFileName); await client.DeleteBucketAsync(bucketName); // delete PDF file from server Directory.Delete(Path.GetDirectoryName(fileSavePath), true); }
/// <summary> /// Deletes the User, Group, and AccessKey which were created for the purposes of /// this example. /// </summary> /// <param name="client">The IAM client used to delete the other /// resources.</param> /// <param name="s3Client">The Amazon S3 client object to delete the /// bucket that was created.</param> /// <param name="userName">The name of the user that will be deleted.</param> /// <param name="groupName">The name of the group that will be deleted.</param> /// <param name="bucketName">The name of the bucket to delete.</param> /// <param name="accessKeyId">The AccessKeyId that represents the /// AccessKey that was created for use with the ListBucketsAsync /// method.</param> public static async Task CleanUpResources( AmazonIdentityManagementServiceClient client, AmazonS3Client s3Client, string userName, string groupName, string bucketName, string accessKeyId) { // Remove the user from the group. var removeUserRequest = new RemoveUserFromGroupRequest() { UserName = userName, GroupName = groupName, }; await client.RemoveUserFromGroupAsync(removeUserRequest); // Delete the client access keys before deleting the user. var deleteAccessKeyRequest = new DeleteAccessKeyRequest() { AccessKeyId = accessKeyId, UserName = userName, }; await client.DeleteAccessKeyAsync(deleteAccessKeyRequest); // Now we can safely delete the user. var deleteUserRequest = new DeleteUserRequest() { UserName = userName, }; await client.DeleteUserAsync(deleteUserRequest); // We have to delete the policy attached to the group first. var deleteGroupPolicyRequest = new DeleteGroupPolicyRequest() { GroupName = groupName, PolicyName = PolicyName, }; await client.DeleteGroupPolicyAsync(deleteGroupPolicyRequest); // Now delete the group. var deleteGroupRequest = new DeleteGroupRequest() { GroupName = groupName, }; await client.DeleteGroupAsync(deleteGroupRequest); // Now delete the bucket. var deleteBucketRequest = new DeleteBucketRequest { BucketName = bucketName, }; await s3Client.DeleteBucketAsync(deleteBucketRequest); Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------"); Console.WriteLine("Deleted the user, the group, and the bucket created for this example."); }
private async Task DeleteBucket() { await s3Client.DeleteObjectAsync(BucketName, FileName); await s3Client.DeleteBucketAsync(BucketName); }