//Button1 - Create Bucket private void button1_Click(object sender, EventArgs e) { bucketName = txtID.Text; var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2); txtOutput.Text += "\r\n"; txtOutput.Text += "Creating Bucket: " + bucketName + "\r\n"; try { PutBucketRequest putRequest1 = new PutBucketRequest { BucketName = bucketName }; PutBucketResponse response1 = client.PutBucket(putRequest1); txtOutput.Text += response1.ToString() + "\r\n"; txtOutput.Text += "Bucket Created: arn:aws:s3:::" + bucketName + "\r\n"; arn = "arn:aws: s3:::" + bucketName; } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { txtOutput.Text += "Check the provided AWS Credentials." + "\r\n"; txtOutput.Text += "For service sign up go to http://aws.amazon.com/s3" + "\r\n"; } else { txtOutput.Text += "Error Creating Bucket!!!!" + amazonS3Exception.Message + "\r\n"; txtOutput.Text += amazonS3Exception.ErrorCode + "\r\n"; } } txtOutput.ScrollToCaret(); }
//private CancellationTokenSource CancellationTokenSource = new CancellationTokenSource(); private bool CreateBucket(string bucketName, Amazon.RegionEndpoint bucketRegion, S3StorageClass storageClass, out string statusMessage) { AWSCredentials awsCredentials = new BasicAWSCredentials(_account.AccessKey, _account.SecretKey); //TransferAgentOptions options = new TransferAgentOptions(); //ITransferAgent transferAgent = new S3TransferAgent(options, awsCredentials, _account.BucketName, CancellationTokenSource.Token); using (var client = new AmazonS3Client(awsCredentials, bucketRegion)) { bool alreadyExists = S3_DoesBucketExist(client, bucketName); if (alreadyExists) { statusMessage = "The informed bucket already exists. Please, pick another name."; return(false); } try { PutBucketRequest putRequest = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true, }; PutBucketResponse response = client.PutBucket(putRequest); switch (response.HttpStatusCode) { default: statusMessage = "Unknown problem."; break; case HttpStatusCode.OK: statusMessage = "The bucket has been successfuly created."; return(true); case HttpStatusCode.Forbidden: statusMessage = string.Format("Bucket creation failed: {0}", response.ToString()); break; } } catch (Exception exception) { if (exception is AmazonS3Exception) { AmazonS3Exception amznException = exception as AmazonS3Exception; if (amznException.ErrorCode != null && (amznException.ErrorCode.Equals("InvalidAccessKeyId") || amznException.ErrorCode.Equals("InvalidSecurity"))) { statusMessage = "Check the provided AWS Credentials."; } else { statusMessage = string.Format("Error occurred. Message:'{0}' when writing an object", amznException.Message); } } else { statusMessage = string.Format("Exception occurred: {0}", exception.Message); } } } return(false); }