예제 #1
0
 public override void CreateFolder(string folder, FileAttributes attributes) {
     folder = folder.Replace('\\', '/');
     PutObjectRequest request = new PutObjectRequest() {
         BucketName = this.bucket,
         Key = this.prefix + folder + '/',
         ContentBody = "",
         StorageClass = this.storageClass,
     };
     this.withHandling(() => {
         request.AddHeader("x-amz-meta-hidden", attributes.HasFlag(FileAttributes.Hidden) ? "true" : "false");
         request.AddHeader("x-amz-meta-archive", attributes.HasFlag(FileAttributes.Archive) ? "true" : "false");
         request.AddHeader("x-amz-meta-not-index", attributes.HasFlag(FileAttributes.NotContentIndexed) ? "true" : "false");
         this.client.PutObject(request);
     }, folder);
     this.folders.Add(folder);
 }
예제 #2
0
        public IList<string> UploadFiles(string path, IList<FileToUpload> files)
        {
            ValidatePath(path);

            var retVal = new List<string>(files.Count);

            using (AmazonS3 client = AWSClientFactory.CreateAmazonS3Client(new AmazonS3Config().WithServiceURL("s3-us-west-1.amazonaws.com")))
            {
                string bucketName = ConfigurationManager.AppSettings[Constants.S3_BUCKET];

                foreach (FileToUpload file in files)
                {
                    var fileUploadRequest = new PutObjectRequest();

                    string key = path + Guid.NewGuid() + Path.GetExtension(file.Filename);

                    fileUploadRequest
                        .WithKey(key)
                        .WithCannedACL(S3CannedACL.PublicRead)
                        .WithBucketName(bucketName)
                        .WithMetaData("Image-Info",file.MetaInfo)
                        .WithInputStream(file.Stream);

                    fileUploadRequest.StorageClass = S3StorageClass.ReducedRedundancy;

                    //"R" - RFC1123 - http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#RFC1123
                    fileUploadRequest.AddHeader("Expires", DateTime.UtcNow.AddYears(10).ToString("R"));
                    fileUploadRequest.AddHeader("Cache-Control", "public, max-age=31536000");

                    using (client.PutObject(fileUploadRequest))
                    {
                    }

                    retVal.Add(string.Format("http://{0}/{1}", bucketName, key));
                }
            }

            return retVal;
        }
예제 #3
0
        private void StoreFile(string file, string key, string bucketName, bool publicRead, bool useGzip)
        {
            S3CannedACL acl = publicRead ? S3CannedACL.PublicRead : S3CannedACL.Private;

            var request = new PutObjectRequest();
            request
                .WithCannedACL(acl)
                .WithFilePath(file)
                .WithBucketName(bucketName)
                .WithKey(key);

            if (useGzip)
            {
                request.AddHeader("Content-Encoding", "gzip");
            }

            Client.PutObject(request);
        }
예제 #4
0
        private void UploadToCdn()
        {
            try
            {
                // one thread only
                if (Interlocked.CompareExchange(ref work, 1, 0) == 0)
                {
                    var @continue = false;
                    try
                    {
                        CdnItem item;
                        if (queue.TryDequeue(out item))
                        {
                            @continue = true;

                            var cdnpath = GetCdnPath(item.Bundle.Path);
                            var key = new Uri(cdnpath).PathAndQuery.TrimStart('/');
                            var etag = AmazonS3Util.GenerateChecksumForContent(item.Response.Content, false).ToLowerInvariant();

                            var config = new AmazonS3Config
                            {
                                ServiceURL = "s3.amazonaws.com",
                                CommunicationProtocol = Protocol.HTTP
                            };
                            using (var s3 = new AmazonS3Client(s3publickey, s3privatekey, config))
                            {
                                var upload = false;
                                try
                                {
                                    var request = new GetObjectMetadataRequest
                                    {
                                        BucketName = s3bucket,
                                        Key = key,
                                    };
                                    using (var response = s3.GetObjectMetadata(request))
                                    {
                                        upload = !string.Equals(etag, response.ETag.Trim('"'), StringComparison.InvariantCultureIgnoreCase);
                                    }
                                }
                                catch (AmazonS3Exception ex)
                                {
                                    if (ex.StatusCode == HttpStatusCode.NotFound)
                                    {
                                        upload = true;
                                    }
                                    else
                                    {
                                        throw;
                                    }
                                }

                                if (upload)
                                {
                                    var request = new PutObjectRequest
                                    {
                                        BucketName = s3bucket,
                                        CannedACL = S3CannedACL.PublicRead,
                                        Key = key,
                                        ContentType = AmazonS3Util.MimeTypeFromExtension(Path.GetExtension(key).ToLowerInvariant()),
                                    };

                                    if (ClientSettings.GZipEnabled)
                                    {
                                        var compressed = new MemoryStream();
                                        using (var compression = new GZipStream(compressed, CompressionMode.Compress, true))
                                        {
                                            new MemoryStream(Encoding.UTF8.GetBytes(item.Response.Content)).CopyTo(compression);
                                        }
                                        request.InputStream = compressed;
                                        request.AddHeader("Content-Encoding", "gzip");
                                    }
                                    else
                                    {
                                        request.ContentBody = item.Response.Content;
                                    }

                                    var cache = TimeSpan.FromDays(365);
                                    request.AddHeader("Cache-Control", string.Format("public, maxage={0}", (int)cache.TotalSeconds));
                                    request.AddHeader("Expires", DateTime.UtcNow.Add(cache).ToString("R"));
                                    request.AddHeader("Etag", etag);
                                    request.AddHeader("Last-Modified", DateTime.UtcNow.ToString("R"));

                                    using (s3.PutObject(request)) { }
                                }

                                item.Bundle.CdnPath = cdnpath;
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        log.Error(err);
                    }
                    finally
                    {
                        work = 0;
                        if (@continue)
                        {
                            Action upload = () => UploadToCdn();
                            upload.BeginInvoke(null, null);
                        }
                    }
                }
            }
            catch (Exception fatal)
            {
                log.Fatal(fatal);
            }
        }
        // Upload entire file
        private void UploadWholeFile(HttpContext context, List<AmazonFilesStatus> statuses)
        {
            for (int i = 0; i < context.Request.Files.Count; i++) {
                var file = context.Request.Files[i];

                MemoryStream thumbStream = new MemoryStream();
                var fileExt = file.FileName.Remove(0,file.FileName.LastIndexOf('.')).ToLower();
                var needsThumb =  Regex.Match(file.FileName.ToLower(),AmazonHelper.ImgExtensions()).Success;
                var keyName = GetKeyName(context,file.FileName.ToLower());
                var len = file.ContentLength;
                var height = 0;
                var width = 0;
                if(needsThumb){

                    file.InputStream.CopyTo(thumbStream);
                }
                var client = AmazonHelper.GetS3Client();
                var request = new PutObjectRequest()
                                        .WithKey(keyName)
                                        .WithBucketName(StorageRoot)
                                        .WithContentType(file.ContentType)
                                        .WithCannedACL(S3CannedACL.PublicRead)
                                        .WithAutoCloseStream(true)
                                        .WithStorageClass(S3StorageClass.ReducedRedundancy)
                                        .WithInputStream(file.InputStream);
                if (needsThumb)
                {
                    Image img = Image.FromStream(thumbStream);
                    height = img.Height;
                    width = img.Width;
                    thumbStream.Position = 0;

                    request.AddHeader("x-amz-meta-width",width.ToString());
                    request.AddHeader("x-amz-meta-height",height.ToString());
                    var orientation = width > height ? "h" : "v";
                    request.AddHeader("x-amz-meta-orient",orientation);

                }

                var response = client.PutObject((PutObjectRequest)request);
                response.Dispose();
                if(needsThumb){

                    var thumbKeyName = VirtualPathUtility.RemoveTrailingSlash(keyName.Replace(file.FileName.ToLower(),"")) + "/thumbs/" + file.FileName.ToLower().Replace(fileExt,".png");
                    var thumbImageStream = GetThumbnail(thumbStream);
                    var thumbrequest = new PutObjectRequest()
                                            .WithKey(thumbKeyName)
                                            .WithBucketName(StorageRoot)
                                            .WithContentType("image/png")
                                            .WithCannedACL(S3CannedACL.PublicRead)
                                            .WithAutoCloseStream(true)
                                            .WithStorageClass(S3StorageClass.ReducedRedundancy)
                                            .WithInputStream(thumbImageStream);

                    var thumbresponse = client.PutObject((PutObjectRequest)thumbrequest);
                    thumbStream.Dispose();
                }
                var fileStatus = new AmazonFilesStatus(file.FileName.ToLower(),StorageRoot,GetPrefix(context),len,height,width );

                //string fullName = Path.GetFileName(file.FileName);
                statuses.Add(fileStatus);
            }
        }
예제 #6
0
파일: AwsHelper.cs 프로젝트: ktj007/Lz
        public void UploadFile(string bucketName, string localPath, string cloudPath)
        {
            var putObjectRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    FilePath = localPath,
                    Key = cloudPath,
                    Timeout = -1,
                    ReadWriteTimeout = 300000
                };

            // make public
            putObjectRequest.AddHeader("x-amz-acl", "public-read");

            S3.PutObject(putObjectRequest);
        }
예제 #7
0
        public override bool CreateFile(string file, string source, DateTime lastModified, string fileMD5, FileAttributes attributes, bool reportProgress=true) {
            file = file.Replace('\\', '/');
            string key = this.prefix + file;

            if (this.files.Contains(file) && this.FileMD5(file) == fileMD5) {
                return false;
            }

            PutObjectRequest putRequest = new PutObjectRequest() {
                BucketName = this.bucket,
                FilePath = source,
                Key = key,
                Timeout = -1,
                CannedACL = S3CannedACL.Private,
                StorageClass = this.storageClass,
            };
            if (reportProgress)
                putRequest.PutObjectProgressEvent += new EventHandler<PutObjectProgressArgs>(putRequest_PutObjectProgressEvent);
            this.withHandling(() => {
                putRequest.AddHeader("x-amz-meta-md5", fileMD5);
                putRequest.AddHeader("x-amz-meta-hidden", attributes.HasFlag(FileAttributes.Hidden) ? "true" : "false");
                putRequest.AddHeader("x-amz-meta-archive", attributes.HasFlag(FileAttributes.Archive) ? "true" : "false");
                putRequest.AddHeader("x-amz-meta-not-index", attributes.HasFlag(FileAttributes.NotContentIndexed) ? "true" : "false");
                this.client.PutObject(putRequest);
            }, file);
            this.files.Add(file);
            return true;
        }