public async Task Initialize() { if (!await AmazonS3Util.DoesS3BucketExistV2Async(_s3Client, _bucket)) { await _s3Client.PutBucketAsync(_bucket); } }
/// <summary> /// Deleting a bucket sending the Name /// </summary> /// <param name="bucketName">Bucket name to delete</param> /// <returns></returns> public async Task <AwsS3Response> DeleteBucketAsync(string bucketName) { try { var existBucket = await AmazonS3Util.DoesS3BucketExistV2Async(_awsS3Client, bucketName); if (!existBucket) { return new AwsS3Response { Message = $"Bucket with name {bucketName} was not found", Status = HttpStatusCode.NotFound } } ; var response = await _awsS3Client.DeleteBucketAsync(bucketName); return(new AwsS3Response { Message = response.ResponseMetadata.RequestId, Status = response.HttpStatusCode }); } catch (AmazonS3Exception e) { return(new AwsS3Response { Message = e.Message, Status = e.StatusCode }); } catch (Exception e) { return(new AwsS3Response { Message = e.Message, Status = HttpStatusCode.InternalServerError }); } }
static async Task CreateBucketAsync() { try { var exists = await AmazonS3Util.DoesS3BucketExistV2Async(s3Client, bucketName); if (!exists) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; PutBucketResponse putBucketResponse = await s3Client.PutBucketAsync(putBucketRequest); } // Retrieve the bucket location. string bucketLocation = await FindBucketLocationAsync(s3Client); } 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 <S3Response> DeleteFileFromFolderAsync(string bucketName, string foldername, string file) { try { if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName) == false) { foldername = foldername.Replace(@"\", "/"); DeleteObjectRequest dor = new DeleteObjectRequest() { BucketName = bucketName, Key = string.Format("{0}/{1}", foldername, file) }; await _client.DeleteObjectAsync(dor); } } catch (AmazonS3Exception e) { return(new S3Response { Message = e.Message, Status = e.StatusCode }); } catch (Exception ex) { return(new S3Response { Message = ex.Message, Status = HttpStatusCode.InternalServerError }); } return(new S3Response { Message = "Something went wrong...", Status = HttpStatusCode.InternalServerError }); }
public (JsonResult result, bool Succeeded, string Error) UploadFile(string bucketName, string base64String, string Path, S3CannedACL s3CannedACL) { try { if (AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName).GetAwaiter().GetResult()) { PutObjectRequest folderRequest = new PutObjectRequest(); var fileTransferUtility = new TransferUtility(_client); byte[] bytes = Convert.FromBase64String(base64String); var fileToUpload = new MemoryStream(bytes); TransferUtilityUploadRequest request = new TransferUtilityUploadRequest(); request.BucketName = bucketName; request.CannedACL = s3CannedACL.Value; request.InputStream = fileToUpload; request.Key = Path; fileTransferUtility.Upload(request); return(new JsonResult("File uploaded successfully."), true, ""); } else { return(new JsonResult("Bucket not exist."), false, "Bucket not exist."); } } catch (Exception ex) { return(new JsonResult("File upload failed."), false, "File upload failed."); } }
public async Task <S3Response> CreateBucketAsync(string bucketName) { try { if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName) == false) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; var response = await _client.PutBucketAsync(putBucketRequest); return(new S3Response { Message = response.ResponseMetadata.RequestId, Status = response.HttpStatusCode }); } } catch (AmazonS3Exception e) { return(new S3Response { Message = e.Message, Status = e.StatusCode }); } catch (Exception ex) { return(new S3Response { Message = ex.Message, Status = HttpStatusCode.InternalServerError }); } return(new S3Response { Message = "Something went wrong...", Status = HttpStatusCode.InternalServerError }); }
public (JsonResult result, bool Succeeded, string Error) CreatBucket(string bucketName) { try { if (AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName).GetAwaiter().GetResult() == false) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; var response = _client.PutBucketAsync(putBucketRequest).GetAwaiter().GetResult(); if (response.HttpStatusCode == HttpStatusCode.OK) { return(new JsonResult("Bucket created successfully."), true, ""); } else { return(new JsonResult("Bucket creation failed."), false, "Bucket creation failed."); } } else { return(new JsonResult("Bucket already exist."), false, "Bucket already exist."); } } catch (Exception ex) { return(new JsonResult("Bucket creation failed."), false, "Bucket creation failed."); } }
public bool CreateS3Bucket(string bucketName) { try { if (!(AmazonS3Util.DoesS3BucketExistV2Async(client, bucketName).Result)) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; PutBucketResponse putBucketResponse = client.PutBucketAsync(putBucketRequest).Result; return(putBucketResponse.HttpStatusCode == System.Net.HttpStatusCode.OK); } } catch (AmazonS3Exception e) { Logger.AddLog(string.Format("Error encountered on server. Message:'{0}' when writing an object", e.Message)); } catch (Exception e) { Logger.AddLog(string.Format("Unknown encountered on server. Message:'{0}' when writing an object", e.Message)); } return(false); }
public async Task <S3Response> CreateLockerAsync(string LockerName) { try { if (await AmazonS3Util.DoesS3BucketExistV2Async(_S3Settings, LockerName) == false) { var putLockerRequest = new PutBucketRequest { BucketName = LockerName, UseClientRegion = true }; var response = await _S3Settings.PutBucketAsync(putLockerRequest); return(new S3Response { Message = response.ResponseMetadata.RequestId, Status = response.HttpStatusCode }); } } catch (AmazonS3Exception e) { return(new S3Response { Status = e.StatusCode, Message = e.Message }); } return(new S3Response { Status = System.Net.HttpStatusCode.InternalServerError, Message = ":(" }); }
public async Task <S3Response> CreateBucketAsync(string name) { try { S3Response Response = null; if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, name) == false) { var bucketrequest = new PutBucketRequest { BucketName = name, UseClientRegion = true }; var bucketresponse = await _client.PutBucketAsync(bucketrequest); Response = new S3Response { Message = bucketresponse.ResponseMetadata.RequestId, status = bucketresponse.HttpStatusCode }; } return(Response); } catch (AmazonS3Exception e1) { return(new S3Response { Message = e1.Message, status = e1.StatusCode }); //Console.WriteLine(e1.StatusCode+"--"+e1.Message); } catch (Exception e1) { return(new S3Response { Message = e1.Message, status = HttpStatusCode.InternalServerError }); //Console.WriteLine(e1.StatusCode+"--"+e1.Message); } }
public async Task <bool> BucketExists(string name) { if (await AmazonS3Util.DoesS3BucketExistV2Async(_amazonS3, name) == true) { return(true); } return(false); }
public async Task <S3Response> DownloadFileAsync(string bucketName, string folderName, string FileName) { string target = Path.GetTempPath(); folderName = folderName.Replace(@"\", "/"); try { if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName) == false) { ListObjectsRequest request = new ListObjectsRequest() { BucketName = bucketName }; do { ListObjectsResponse response = await _client.ListObjectsAsync(request); for (int i = 1; i <= response.S3Objects.Count - 1; i++) { S3Object entry = response.S3Objects[i]; if (entry.Key.Replace(folderName + "/", "") == FileName) { GetObjectRequest objRequest = new GetObjectRequest() { BucketName = bucketName, Key = entry.Key }; GetObjectResponse objResponse = await _client.GetObjectAsync(objRequest); await objResponse.WriteResponseStreamToFileAsync(target + FileName, true, CancellationToken.None); break; } } if ((response.IsTruncated)) { request.Marker = response.NextMarker; } else { request = null /* TODO Change to default(_) if this is not a reference type */; } }while (request != null); } } catch (AmazonS3Exception e) { return(new S3Response { Message = e.Message, Status = e.StatusCode }); } catch (Exception ex) { return(new S3Response { Message = ex.Message, Status = HttpStatusCode.InternalServerError }); } return(new S3Response { Message = "Something went wrong...", Status = HttpStatusCode.InternalServerError }); }
public override async Task <bool> HasBucket(ILogger log, CancellationToken token) { if (_hasBucket == null) { _hasBucket = await AmazonS3Util.DoesS3BucketExistV2Async(_client, _bucketName); } return(_hasBucket == true); }
public override async Task ActivatingAsync() { if (_options.CreateBucket && _shellSettings.State != Environment.Shell.Models.TenantState.Uninitialized && !String.IsNullOrEmpty(_options.BucketName)) { _logger.LogDebug("Testing Amazon S3 Bucket {BucketName} existence", _options.BucketName); try { var isBucketExists = await AmazonS3Util.DoesS3BucketExistV2Async(_amazonS3Client, _options.BucketName); if (isBucketExists) { _logger.LogInformation("Amazon S3 Bucket {BucketName} already exists.", _options.BucketName); return; } var bucketRequest = new PutBucketRequest { BucketName = _options.BucketName, UseClientRegion = true }; // Tying to create bucket var response = await _amazonS3Client.PutBucketAsync(bucketRequest); if (!response.IsSuccessful()) { _logger.LogError("Unable to create Amazon S3 Bucket. {Response}", response); return; } // Blocking public access for the newly created bucket. var blockConfiguration = new PublicAccessBlockConfiguration { BlockPublicAcls = true, BlockPublicPolicy = true, IgnorePublicAcls = true, RestrictPublicBuckets = true }; await _amazonS3Client.PutPublicAccessBlockAsync(new PutPublicAccessBlockRequest { PublicAccessBlockConfiguration = blockConfiguration, BucketName = _options.BucketName }); _logger.LogDebug("Amazon S3 Bucket {BucketName} created.", _options.BucketName); } catch (Exception e) { _logger.LogError(e, "Unable to create Amazon S3 Bucket."); } } }
protected virtual async Task CreateContainerIfNotExists(AmazonS3Client amazonS3Client, string containerName) { if (!await AmazonS3Util.DoesS3BucketExistV2Async(amazonS3Client, containerName)) { await amazonS3Client.PutBucketAsync(new PutBucketRequest { BucketName = containerName }); } }
public async static Task <string> CreateBucketAsync(IAmazonS3 s3Client, string testName) { string bucketName = string.Format("{0}-{1}-{2}", SDK_TEST_PREFIX, testName, DateTime.Now.Ticks).ToLower().Replace('_', '-'); await s3Client.PutBucketAsync(new PutBucketRequest { BucketName = bucketName }); while (!await AmazonS3Util.DoesS3BucketExistV2Async(s3Client, bucketName)) { await Task.Delay(500); } return(bucketName); }
public async static Task <string> CreateBucketAsync(IAmazonS3 s3Client) { string bucketName = $"{SDK_TEST_PREFIX}-{Guid.NewGuid()}"; await s3Client.PutBucketAsync(new PutBucketRequest { BucketName = bucketName }); while (!await AmazonS3Util.DoesS3BucketExistV2Async(s3Client, bucketName)) { await Task.Delay(500); } return(bucketName); }
public async Task <bool> DoesBucketExistAsync(string bucketName) { try { return(await AmazonS3Util.DoesS3BucketExistV2Async(_amazonS3Client, bucketName.ToLower())); } catch (Exception e) { _logger.LogError(e.Message); throw; } }
public async Task <ExecutionResponse <object> > CreateBucketAsync(string bucketName) { try { if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName) == false) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; var response = await _client.PutBucketAsync(putBucketRequest); return(new ExecutionResponse <object> { ResponseCode = ResponseCode.Ok, ResponseData = response.ResponseMetadata.RequestId }); } return(new ExecutionResponse <object> { ResponseCode = ResponseCode.Ok, ResponseData = "Bucket already exist" }); } catch (AmazonS3Exception ex) { Log.Error(ex); return(new ExecutionResponse <object> { ResponseCode = ResponseCode.ServerException, Message = ex.Message }); } catch (Exception ex) { Log.Error(ex); return(new ExecutionResponse <object> { ResponseCode = ResponseCode.ServerException, Message = $"An error occured - {ex.Message}" }); } return(new ExecutionResponse <object> { ResponseCode = ResponseCode.ServerException, Message = "Something went wrong" }); }
public override async Task CreateContainer(CancellationToken cancellationToken) { if (!await AmazonS3Util.DoesS3BucketExistV2Async(_s3Client.Value, Container).ConfigureAwait(false)) { var putBucketRequest = new PutBucketRequest { BucketName = Container, UseClientRegion = true }; PutBucketResponse putBucketResponse = await _s3Client.Value.PutBucketAsync(putBucketRequest, cancellationToken).ConfigureAwait(false); } }
public async Task <S3Response> CreateBucketAsync(string bucketName) { Console.WriteLine("CreateBucket"); Console.WriteLine(bucketName); try { if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName) == false) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; var response = await _client.PutBucketAsync(putBucketRequest); return(new S3Response { Message = response.ResponseMetadata.RequestId, Status = response.HttpStatusCode }); } } catch (AmazonS3Exception e) { Console.WriteLine(e); return(new S3Response { Message = e.Message, Status = e.StatusCode }); } catch (Exception e) { Console.WriteLine(e); return(new S3Response { Message = e.Message, Status = HttpStatusCode.InternalServerError }); } return(new S3Response { Message = "Algo salio mal", Status = HttpStatusCode.InternalServerError }); }
public async Task CreateBucketAsync() { if (AmazonS3Util.DoesS3BucketExistV2Async(_client, BucketName).Result == false) { var putBucketRequest = new PutBucketRequest { BucketName = BucketName, UseClientRegion = true }; var response = await _client.PutBucketAsync(putBucketRequest); _logger.LogInformation("Created Bucket - BucketName: {0}", BucketName); } else { _logger.LogInformation("Bucket is exist - BucketName: {0}", BucketName); } }
/// <summary> /// Creates enw S3 bucket with CORS to my site /// </summary> /// <param name="bucketName"></param> /// <returns></returns> public async Task <S3Response> CreateBucketAsync(string bucketName) { try { //Check if bucket already exists if (!await AmazonS3Util.DoesS3BucketExistV2Async(_s3Client, bucketName)) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true, }; var response = await _s3Client.PutBucketAsync(putBucketRequest); //Enable CORS For bucket S3Cors cors = new S3Cors(bucketName); return(new S3Response { Message = response.ResponseMetadata.RequestId, StatusCode = response.HttpStatusCode }); } } catch (AmazonS3Exception ex) { return(new S3Response { StatusCode = ex.StatusCode, Message = ex.Message }); } catch (Exception ex) { Console.WriteLine(ex); } return(new S3Response { StatusCode = System.Net.HttpStatusCode.InternalServerError, Message = "Error on the server side" }); }
public async Task <S3Response> CreateBucketAsync(string kovaAdi) { try { if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, kovaAdi) == false) { var putBucketRequest = new PutBucketRequest { BucketName = kovaAdi, UseClientRegion = true }; var response = await _client.PutBucketAsync(putBucketRequest); return(new S3Response { Message = response.ResponseMetadata.RequestId, Status = response.HttpStatusCode }); } } catch (AmazonS3Exception s3ex) { return(new S3Response { Status = s3ex.StatusCode, Message = s3ex.Message }); } catch (Exception ex) { return(new S3Response { Status = HttpStatusCode.InternalServerError, Message = ex.Message }); } return(new S3Response { Status = HttpStatusCode.InternalServerError, Message = "Bilinmeyen hata" }); }
internal async Task CreateBucketIfNotExists(string bucketName) { var exists = await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName); if (!exists) { try { await _client.PutBucketAsync(bucketName).ConfigureAwait(false); } catch (AmazonS3Exception ex) { if (ex.ErrorCode != BucketAlreadyOwnedByYouError) { throw; } } } }
public async Task CreateBucketAsync(string bucketName) { if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName) == false) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, BucketRegionName = _regionName, }; var response = await _client.PutBucketAsync(putBucketRequest); var t = new { Message = response.ResponseMetadata.RequestId, Status = response.HttpStatusCode }; } }
/// <summary> /// Create a bucket sending the Name /// </summary> /// <param name="bucketName">Bucket Name (string)</param> /// <returns></returns> public async Task <AwsS3Response> CreateBucketAsync(string bucketName) { try { var existBucket = await AmazonS3Util.DoesS3BucketExistV2Async(_awsS3Client, bucketName); if (existBucket) { return new AwsS3Response { Message = "The bucket already exists", Status = HttpStatusCode.Conflict } } ; var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; var response = await _awsS3Client.PutBucketAsync(putBucketRequest); return(new AwsS3Response { Message = $"Bucket with name {bucketName} was successfully created", Status = response.HttpStatusCode }); } catch (AmazonS3Exception e) { return(new AwsS3Response { Message = e.Message, Status = e.StatusCode }); } catch (Exception e) { return(new AwsS3Response { Message = e.Message, Status = HttpStatusCode.InternalServerError }); } }
public async Task <S3Response> UploadFileToFolderAsync(IFormFile file, string bucketName, string folderName) { try { if (await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName) == false) { await CreateFolderAsync(bucketName, folderName); } else { byte[] fileBytes = new Byte[file.Length]; file.OpenReadStream().Read(fileBytes, 0, Int32.Parse(file.Length.ToString())); // create unique file name for prevent the mess var fileName = Guid.NewGuid() + file.FileName; string key = string.Format("{0}/{1}", folderName, file.Name); PutObjectRequest por = new PutObjectRequest(); using (var stream = new MemoryStream(fileBytes)) { por.BucketName = bucketName; por.StorageClass = S3StorageClass.Standard; por.ServerSideEncryptionMethod = ServerSideEncryptionMethod.None; por.CannedACL = S3CannedACL.PublicRead; por.Key = key; por.InputStream = stream; var response = await _client.PutObjectAsync(por); return(new S3Response { Message = response.ResponseMetadata.RequestId, Status = response.HttpStatusCode }); } } } catch (AmazonS3Exception e) { return(new S3Response { Message = e.Message, Status = e.StatusCode }); } return(new S3Response { Message = "Something went wrong...", Status = HttpStatusCode.InternalServerError }); }
private async Task <string> CreateBucketAsync() { string ret; try { string bucketStatus; if (await AmazonS3Util.DoesS3BucketExistV2Async(_s3Client, BUCKET_NAME)) { bucketStatus = $"Bucket '{BUCKET_NAME}' already exists "; } else { var putBucketRequest = new PutBucketRequest { BucketName = BUCKET_NAME, UseClientRegion = true }; PutBucketResponse putBucketResponse = await _s3Client.PutBucketAsync(putBucketRequest); bucketStatus = $"New bucket '{BUCKET_NAME}' is created "; } // Retrieve the bucket location. string bucketLocation = await FindBucketLocationAsync(); string region = RegionEndpoint.GetBySystemName(bucketLocation).DisplayName; ret = $"{bucketStatus} in the region of {region}."; } catch (AmazonS3Exception e) { ret = $"Error encountered on server. Message:'{e.Message}' when writing an object"; } catch (Exception e) { ret = $"Unknown error encountered on server. Message:'{e.Message}' when writing an object"; } return(ret); }
private async Task CreateBucketAsync(string bucketName) { try { var bucketExists = await AmazonS3Util.DoesS3BucketExistV2Async(_client, bucketName); if (!bucketExists) { var putBucketRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true }; await _client.PutBucketAsync(putBucketRequest); } } catch (Exception ex) { _logger.LogError(ex, ex.Message); throw; } }