Ejemplo n.º 1
0
        //removes a file in S3 cloud
        public static void RemoveObjectFromS3(string filePath)
        {
            AmazonS3 client;

            if (CheckS3Credentials())
            {
                NameValueCollection appConfig =
                    ConfigurationManager.AppSettings;

                string accessKeyID       = appConfig["AWSAccessKey"];
                string secretAccessKeyID = appConfig["AWSSecretKey"];

                using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                           accessKeyID, secretAccessKeyID, RegionEndpoint.USWest1))
                {
                    try
                    {
                        DeleteObjectRequest request = new DeleteObjectRequest();
                        request.WithBucketName(Constants.AmazonS3BucketName)
                        .WithKey(filePath);

                        S3Response response = client.DeleteObject(request);
                        response.Dispose();
                    }
                    catch (AmazonS3Exception amazonS3Exception)
                    {
                        if (amazonS3Exception.ErrorCode != null &&
                            (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                             ||
                             amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                        {
                            Console.WriteLine("Check the provided AWS Credentials.");
                            Console.WriteLine(
                                "For service sign up go to http://aws.amazon.com/s3");
                        }
                        else
                        {
                            Console.WriteLine(
                                "Error occurred. Message:'{0}' when deleting an object"
                                , amazonS3Exception.Message);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        static void WritingAnObject()
        {
            try
            {
                // simple object put
                PutObjectRequest request = new PutObjectRequest();
                request.WithContentBody("this is a test")
                .WithBucketName(bucketName)
                .WithKey(keyName);

                S3Response response = client.PutObject(request);
                response.Dispose();

                // put a more complex object with some metadata and http headers.
                PutObjectRequest titledRequest = new PutObjectRequest();
                titledRequest.WithMetaData("title", "the title")
                .WithContentBody("this object has a title")
                .WithBucketName(bucketName)
                .WithKey(keyName);

                using (S3Response responseWithMetadata = client.PutObject(titledRequest))
                {
                    WebHeaderCollection headers = response.Headers;
                    foreach (string key in headers.Keys)
                    {
                        Console.WriteLine("Response Header: {0}, Value: {1}", key, headers.Get(key));
                    }
                }
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                     amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Please check the provided AWS Credentials.");
                    Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message);
                }
            }
        }
Ejemplo n.º 3
0
        public static void WriteObjectRequest(string bucketName, string fileName, Stream fileContent, AmazonS3Client s3Client)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                return;
            }

            PutObjectRequest putObjectRequest = new PutObjectRequest();

            putObjectRequest.WithBucketName(bucketName)
            .WithKey(fileName)
            .WithStorageClass(S3StorageClass.Standard)
            .WithCannedACL(S3CannedACL.PublicRead)
            .WithInputStream(fileContent);


            S3Response response = s3Client.PutObject(putObjectRequest);

            response.Dispose();
        }
Ejemplo n.º 4
0
        public static String UploadToS3(string fileName, Stream fileContent)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                return("");
            }

            AmazonS3 s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client("AKIAJ4A6DAATIDU6ELAA", "EiA6EILkCp7pqzvnIUhXg3FFOft0j+pA/DtBM8if");

            PutObjectRequest putObjectRequest = new PutObjectRequest();

            putObjectRequest.WithBucketName("shriners_rms");
            putObjectRequest.CannedACL   = S3CannedACL.PublicRead;
            putObjectRequest.Key         = fileName;
            putObjectRequest.InputStream = fileContent;
            S3Response response = s3Client.PutObject(putObjectRequest);

            response.Dispose();

            return("http://shriners_rms.s3.amazonaws.com/" + fileName);
        }
Ejemplo n.º 5
0
        //removes all files with same prefix in S3 cloud
        public static void RemoveAllObjectsWithPrefixS3(string filePath)
        {
            AmazonS3 client;

            if (CheckS3Credentials())
            {
                NameValueCollection appConfig =
                    ConfigurationManager.AppSettings;

                string accessKeyID       = appConfig["AWSAccessKey"];
                string secretAccessKeyID = appConfig["AWSSecretKey"];

                using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                           accessKeyID, secretAccessKeyID, RegionEndpoint.USWest1))
                {
                    try
                    {
                        //first get all objects with the same prefix (in the same "folder")
                        ListObjectsRequest listRequest = new ListObjectsRequest();
                        listRequest.WithBucketName(Constants.AmazonS3BucketName)
                        .WithPrefix(filePath);

                        ListObjectsResponse listResponse = client.ListObjects(listRequest);

                        //delete all objects
                        DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest();
                        deleteRequest.WithBucketName(Constants.AmazonS3BucketName);

                        foreach (S3Object obj in listResponse.S3Objects)
                        {
                            deleteRequest.AddKey(obj.Key);
                        }

                        //if there are any files to delete
                        if (deleteRequest.Keys.Count > 0)
                        {
                            S3Response response = client.DeleteObjects(deleteRequest);
                            response.Dispose();
                        }
                    }
                    catch (AmazonS3Exception amazonS3Exception)
                    {
                        if (amazonS3Exception.ErrorCode != null &&
                            (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                             ||
                             amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                        {
                            Console.WriteLine("Check the provided AWS Credentials.");
                            Console.WriteLine(
                                "For service sign up go to http://aws.amazon.com/s3");
                        }
                        else
                        {
                            Console.WriteLine(
                                "Error occurred. Message:'{0}' when deleting an object"
                                , amazonS3Exception.Message);
                        }
                    }
                }
            }
        }