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);
        }
Beispiel #2
0
        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);
                }
            }
        }
Beispiel #3
0
    public ArrayList GetAppImageArchiveUrls(Hashtable State)
    {
        ArrayList raw_image_list = new ArrayList();
        ArrayList image_list = new ArrayList();
        string AWSAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
        string AWSSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];
        string Bucket = ConfigurationManager.AppSettings["ImageBucket"];
        Amazon.S3.AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey);
        try
        {
            ListObjectsRequest request = new ListObjectsRequest();
            request.BucketName = Bucket;
            string prefix = "https://s3.amazonaws.com/" + Bucket + "/";
            request.WithPrefix(State["Username"].ToString());
            raw_image_list = GetImages(client, request, prefix, raw_image_list);
            string app_string = "/" + State["SelectedApp"].ToString().Replace(" ","_") + "/";
            foreach (string url in raw_image_list)
            {
                if ( url.Contains(app_string))
                {
                    image_list.Add(url);
                }
            }
        }
        catch (AmazonS3Exception ex)
        {
            Util util = new Util();
            util.LogError(State, ex);
            throw new Exception(ex.Message + ": " + ex.StackTrace);
        }

        return image_list;
    }
Beispiel #4
0
    public ArrayList GetStockImageUrls(Hashtable State)
    {
        ArrayList image_list = new ArrayList();
        string AWSAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
        string AWSSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];
        string Bucket = ConfigurationManager.AppSettings["ImageBucket"];
        Amazon.S3.AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey);
        try
        {
            ListObjectsRequest request = new ListObjectsRequest();
            request.BucketName = Bucket;

            // list only things starting with "apps/images"

            string prefix = "https://s3.amazonaws.com/" + Bucket + "/";
            request.WithPrefix("apps/images/bars");
            image_list = GetImages(client, request, prefix, image_list);

            request.WithPrefix("apps/images/blank_buttons");
            image_list = GetImages(client, request, prefix, image_list);

            request.WithPrefix("apps/images/jquery_buttons");
            image_list = GetImages(client, request, prefix, image_list);

            request.WithPrefix("apps/images/bw_dark_icons");
            image_list = GetImages(client,request,prefix, image_list);

            request.WithPrefix("apps/images/bw_light_icons");
            image_list = GetImages(client, request, prefix, image_list);

            request.WithPrefix("apps/images/icon_buttons");
            image_list = GetImages(client, request, prefix, image_list);

            request.WithPrefix("apps/images/navigation_buttons");
            image_list = GetImages(client, request, prefix, image_list);

            request.WithPrefix("apps/images/ratings");
            image_list = GetImages(client, request, prefix, image_list);
        }
        catch (AmazonS3Exception ex)
        {
            Util util = new Util();
            util.LogError(State,ex);
            throw new Exception( ex.Message + ": " + ex.StackTrace);
        }

        return image_list;
    }
Beispiel #5
0
 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;
     }
 }
Beispiel #6
0
    public ArrayList GetAccountImageArchiveUrls(Hashtable State)
    {
        ArrayList raw_image_list = new ArrayList();
        ArrayList image_list = new ArrayList();
        string AWSAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
        string AWSSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];
        string Bucket = ConfigurationManager.AppSettings["ImageBucket"];
        Amazon.S3.AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey);
        try
        {
            ListObjectsRequest request = new ListObjectsRequest();
            request.BucketName = Bucket;

            // list only things starting with "apps/images"

            string prefix = "https://s3.amazonaws.com/" + Bucket + "/";
            request.WithPrefix(State["Username"].ToString());
            raw_image_list = GetImages(client, request, prefix, raw_image_list);
            foreach (string url in raw_image_list)
            {
                if (url.EndsWith(".jpg") || url.EndsWith(".png") || url.EndsWith(".gif"))
                {
                    image_list.Add(url);
                }
            }
        }
        catch (AmazonS3Exception ex)
        {
            Util util = new Util();
            util.LogError(State, ex);
            throw new Exception(ex.Message + ": " + ex.StackTrace);
        }

        return image_list;
    }
Beispiel #7
0
        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)));
                    }
                }
            }
        }
Beispiel #8
0
 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);
         }
     }
 }
Beispiel #9
0
 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;
         }
     }
 }
Beispiel #10
0
        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));
                    }
                }
            }
        }
        public IEnumerable<StorageEntry> GetEntries(string path, string filter)
        {
            var list = new List<StorageEntry>();
            using (var s3 = GetAmazonS3())
            {
                var listRequest = new ListObjectsRequest { BucketName = path };
                listRequest.WithPrefix(filter);
                using (var listResponse = s3.ListObjects(listRequest))
                {
                    foreach (var entry in listResponse.S3Objects)
                    {
                        var cachePath = Path.Combine(_storagePath, entry.Key);

                        var request = new GetObjectRequest().WithBucketName(BucketName).WithKey(entry.Key);
                        using (var response = s3.GetObject(request))
                        {
                            if (!File.Exists(cachePath))
                            {
                                response.WriteResponseStreamToFile(cachePath);
                            }
                        }

                        list.Add(this.ToStorageEntry(cachePath));
                    }
                }
            }

            return list;
        }