Ejemplo n.º 1
0
        public Stream GetFile(string folderName, string fileName, bool useCache)
        {
            //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");
            }

            if (useCache && !string.IsNullOrWhiteSpace(clientContext.PackagesUrl))
            {
                var         url      = new Uri(string.Format("{0}/{1}", clientContext.PackagesUrl, fileName));
                WebRequest  request  = WebRequest.Create(url);
                WebResponse response = request.GetResponse();

                return(response.GetResponseStream());
            }
            else
            {
                var request = new GetObjectRequest();
                request.WithBucketName(clientContext.BucketName);
                request.WithKey(fileName);
                request.WithTimeout((int)TimeSpan.FromMinutes(30).TotalMilliseconds);

                using (AmazonS3 client = clientContext.CreateInstance())
                {
                    try
                    {
                        S3Response response = WrapRequestInErrorHandler(() => client.GetObject(request));

                        if (response != null)
                        {
                            return(response.ResponseStream);
                        }
                    }
                    catch (Exception)
                    {
                        //hate swallowing an error
                    }

                    return(null);
                }
            }
        }
        public Stream get_file(string folderName, string fileName, bool useCache)
        {
            // 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);

            if (useCache && !string.IsNullOrWhiteSpace(clientContext.ImagesUrl))
            {
                var         url      = new Uri(string.Format("{0}/{1}", clientContext.ImagesUrl, fileName));
                WebRequest  request  = WebRequest.Create(url);
                WebResponse response = request.GetResponse();

                return(response.GetResponseStream());
            }
            else
            {
                var request = new GetObjectRequest();
                request.WithBucketName(clientContext.BucketName);
                request.WithKey(fileName);
                request.WithTimeout((int)TimeSpan.FromMinutes(30).TotalMilliseconds);

                using (AmazonS3 client = clientContext.create_instance())
                {
                    try
                    {
                        S3Response response = wrap_request_in_error_handler(() => client.GetObject(request));

                        if (response != null)
                        {
                            return(response.ResponseStream);
                        }
                    }
                    catch (Exception)
                    {
                        //hate swallowing an error
                    }

                    return(null);
                }
            }
        }