コード例 #1
0
ファイル: S3Sample.cs プロジェクト: windygu/ntlxproject
        static void ListingObjects()
        {
            try
            {
                ListObjectsRequest request = new ListObjectsRequest();
                request.BucketName = bucketName;
                using (ListObjectsResponse response = client.ListObjects(request))
                {
                    foreach (S3Object entry in response.S3Objects)
                    {
                        Console.WriteLine("key = {0} size = {1}", entry.Key, entry.Size);
                    }
                }

                // list only things starting with "foo"
                request.WithPrefix("foo");
                using (ListObjectsResponse response = client.ListObjects(request))
                {
                    foreach (S3Object entry in response.S3Objects)
                    {
                        Console.WriteLine("key = {0} size = {1}", entry.Key, entry.Size);
                    }
                }

                // list only things that come after "bar" alphabetically
                request.WithPrefix(null)
                .WithMarker("bar");
                using (ListObjectsResponse response = client.ListObjects(request))
                {
                    foreach (S3Object entry in response.S3Objects)
                    {
                        Console.WriteLine("key = {0} size = {1}", entry.Key, entry.Size);
                    }
                }

                // only list 3 things
                request.WithPrefix(null)
                .WithMarker(null)
                .WithMaxKeys(3);
                using (ListObjectsResponse response = client.ListObjects(request))
                {
                    foreach (S3Object entry in response.S3Objects)
                    {
                        Console.WriteLine("key = {0} size = {1}", entry.Key, entry.Size);
                    }
                }
            }
            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 listing objects", amazonS3Exception.Message);
                }
            }
        }
コード例 #2
0
        public bool FileExists(string folderName, string fileName)
        {
            //folder ignored - packages stored on top level of S3 bucket
            if (String.IsNullOrWhiteSpace(folderName))
            {
                throw new ArgumentNullException("folderName");
            }
            if (String.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            var request = new ListObjectsRequest();

            request.WithBucketName(clientContext.BucketName);
            request.WithPrefix(fileName);

            using (AmazonS3 client = clientContext.CreateInstance())
            {
                ListObjectsResponse response = WrapRequestInErrorHandler(() => client.ListObjects(request));
                var count = response.S3Objects.Count;
                if (count == 1)
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #3
0
        public bool file_exists(string folderName, string fileName)
        {
            // It's allowed to have an empty folder name.
            // if (String.IsNullOrWhiteSpace(folderName)) throw new ArgumentNullException("folderName");
            if (String.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            folderName = (string.IsNullOrEmpty(folderName) ? String.Empty : folderName.Substring(folderName.Length - 1, 1) == "/" ? folderName : folderName + "/");
            fileName   = string.Format("{0}{1}", folderName, fileName);

            var request = new ListObjectsRequest();

            request.WithBucketName(clientContext.BucketName);
            request.WithPrefix(fileName);

            using (AmazonS3 client = clientContext.create_instance())
            {
                ListObjectsResponse response = wrap_request_in_error_handler(() => client.ListObjects(request));
                var count = response.S3Objects.Count;
                if (count == 1)
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #4
0
ファイル: S3Measurement.cs プロジェクト: yarivat/Admin
        protected virtual long GetFolderSize(AmazonS3 client, string folderPath, string bucketName)
        {
            ListObjectsRequest request = new ListObjectsRequest();

            request.WithBucketName(bucketName);
            request.WithPrefix(folderPath);
            long total = 0;

            do
            {
                ListObjectsResponse response = client.ListObjects(request);

                if (response != null && response.S3Objects != null)
                {
                    total += response.S3Objects.Sum(s => s.Size);
                }

                if (response.IsTruncated)
                {
                    request.Marker = response.NextMarker;
                }
                else
                {
                    request = null;
                }
            } while (request != null);

            return(total);
        }
コード例 #5
0
ファイル: S3Storage.cs プロジェクト: Wifisoft/teamlab.v6.5
        public override void CopyDirectory(string srcdomain, string srcdir, string newdomain, string newdir)
        {
            string srckey = MakePath(srcdomain, srcdir);
            string dstkey = MakePath(newdomain, newdir);

            //List files from src
            using (AmazonS3 client = GetClient())
            {
                var request = new ListObjectsRequest {
                    BucketName = _bucket
                };
                request.WithPrefix(srckey);

                using (ListObjectsResponse response = client.ListObjects(request))
                {
                    foreach (S3Object s3Object in response.S3Objects)
                    {
                        if (QuotaController != null)
                        {
                            QuotaController.QuotaUsedAdd(_modulename, newdomain, _dataList.GetData(newdomain), s3Object.Size);
                        }

                        client.CopyObject(new CopyObjectRequest()
                                          .WithSourceBucket(_bucket)
                                          .WithSourceKey(s3Object.Key)
                                          .WithDestinationBucket(_bucket)
                                          .WithDestinationKey(s3Object.Key.Replace(srckey, dstkey)).WithCannedACL(
                                              GetDomainACL(newdomain)));
                    }
                }
            }
        }
コード例 #6
0
ファイル: S3Storage.cs プロジェクト: Wifisoft/teamlab.v6.5
        public override void MoveDirectory(string srcdomain, string srcdir, string newdomain, string newdir)
        {
            string srckey = MakePath(srcdomain, srcdir);
            string dstkey = MakePath(newdomain, newdir);

            //List files from src
            using (AmazonS3 client = GetClient())
            {
                var request = new ListObjectsRequest {
                    BucketName = _bucket
                };
                request.WithPrefix(srckey);

                using (ListObjectsResponse response = client.ListObjects(request))
                {
                    foreach (S3Object s3Object in response.S3Objects)
                    {
                        client.CopyObject(new CopyObjectRequest()
                                          .WithSourceBucket(_bucket)
                                          .WithSourceKey(s3Object.Key)
                                          .WithDestinationBucket(_bucket)
                                          .WithDestinationKey(s3Object.Key.Replace(srckey, dstkey)).WithCannedACL(
                                              GetDomainACL(newdomain)));
                        client.DeleteObject(new DeleteObjectRequest().WithBucketName(_bucket).WithKey(s3Object.Key));
                    }
                }
            }
        }
コード例 #7
0
        //------------------------------------------
        #endregion
        #endregion

        #region --------------FileMethods--------------


        #region --------------DoesFileExist--------------
        public bool DoesFileExist(string bucketName, string filefullPath)
        {
            ListObjectsRequest request = new ListObjectsRequest();

            request.BucketName = bucketName;
            request.WithPrefix(filefullPath);
            request.MaxKeys = 1;
            using (ListObjectsResponse response = S3Client.ListObjects(request))
            {
                return(response.S3Objects.Count > 0);
            }
        }
コード例 #8
0
ファイル: S3Storage.cs プロジェクト: Wifisoft/teamlab.v6.5
 public override bool IsFile(string domain, string path)
 {
     using (AmazonS3 client = GetClient())
     {
         var request = new ListObjectsRequest {
             BucketName = _bucket
         };
         request.WithPrefix(MakePath(domain, path));
         using (ListObjectsResponse response = client.ListObjects(request))
         {
             return(response.S3Objects.Count > 0);
         }
     }
 }
コード例 #9
0
ファイル: S3Storage.cs プロジェクト: Wifisoft/teamlab.v6.5
 public override long GetFileSize(string domain, string path)
 {
     using (AmazonS3 client = GetClient())
     {
         var request = new ListObjectsRequest {
             BucketName = _bucket
         };
         request.WithPrefix(MakePath(domain, path));
         using (ListObjectsResponse response = client.ListObjects(request))
         {
             if (response.S3Objects.Count > 0)
             {
                 return(response.S3Objects[0].Size);
             }
             throw new FileNotFoundException("file not found", path);
         }
     }
 }
コード例 #10
0
        public ActionResult Index()
        {
            // get me all objects inside a given folder
            Dictionary <string, double> images = null;

            var request = new ListObjectsRequest();

            request.BucketName = AWSBucket;
            request.WithPrefix(AWSFolder);

            using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey))
            {
                using (ListObjectsResponse response = client.ListObjects(request))
                {
                    images = response.S3Objects.Where(x => x.Key != AWSFolder).ToDictionary(obj => obj.Key, obj => AppHelper.ConvertBytesToMegabytes(obj.Size));
                }
            }

            return(View(images));
        }
コード例 #11
0
ファイル: S3Storage.cs プロジェクト: Wifisoft/teamlab.v6.5
 private IEnumerable <S3Object> GetS3Objects(string domain, string path)
 {
     using (AmazonS3 client = GetClient())
     {
         var request = new ListObjectsRequest {
             BucketName = _bucket
         };
         request.WithPrefix(MakePath(domain, path.TrimEnd('/') + '/')).WithMaxKeys(1000);
         var objects = new List <S3Object>();
         ListObjectsResponse response;
         do
         {
             response = client.ListObjects(request);
             objects.AddRange(response.S3Objects.Where(entry => CheckKey(domain, entry.Key)));
             if (objects.Count > 0)
             {
                 request.Marker = objects[objects.Count - 1].Key;
             }
         } while (response.IsTruncated);
         return(objects);
     }
 }