Mimics the System.IO.FileInfo for a file in S3. It exposes properties and methods manipulating files in S3.
Inheritance: IS3FileSystemInfo
        public void Test()
        {
            /*
            var x = "woof/bubu/x";
            var dir = new S3DirectoryInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, "a/b");
            dir.Create();
            Console.WriteLine("Name: " + dir.Name + ", FullName: " + dir.FullName);

            
            var dir2 = new S3DirectoryInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, "1\\2");
            dir2.Create();
            Console.WriteLine("Name: " + dir2.Name + ", FullName: " + dir2.FullName);
            var file = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, "1\\2\\t.txt");
            using (file.Create()) { }
            Console.WriteLine("Name: {0}, FullName: {1}, DirName: {2}", file.Name, file.FullName, file.DirectoryName);
            var file2 = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, "a/b/t2.txt");

            using (file2.Create()) { }
            Console.WriteLine("Name: {0}, FullName: {1}, DirName: {2}", file2.Name, file2.FullName, file2.DirectoryName);
            =*/
            using (var fsFileStream = File.OpenRead(@"E:\al\pics\adventure_time\x.jpg"))
            {
                var a3File = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, @"test\x2.jpg");
                using (a3File.Create()) { }
                
                using(var outStream = a3File.OpenWrite())
                {
                    fsFileStream.CopyTo(outStream);
                }
                PublishFile(a3File.FullName);
            }
        }
示例#2
0
        /// <summary>
        /// Moves the file to a a new location in S3.
        /// </summary>
        /// <param name="bucket">Bucket to move the file to.</param>
        /// <param name="key">Object key to move the file to.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3.</exception>
        /// <exception cref="T:System.ArgumentException"></exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo for the target location.</returns>
        public S3FileInfo MoveTo(string bucket, string key)
        {
            S3FileInfo ret = CopyTo(bucket, key, false);

            Delete();
            return(ret);
        }
示例#3
0
        /// <summary>
        /// Moves the file to a a new location in S3.
        /// </summary>
        /// <param name="file">The target file to copy to.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3.</exception>
        /// <exception cref="T:System.ArgumentException"></exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo for the target location.</returns>
        public S3FileInfo MoveTo(S3FileInfo file)
        {
            S3FileInfo ret = CopyTo(file, false);

            Delete();
            return(ret);
        }
示例#4
0
        /// <summary>
        /// Moves the file to a a new location in S3.
        /// </summary>
        /// <param name="path">The target directory to copy to.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3.</exception>
        /// <exception cref="T:System.ArgumentException"></exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo for the target location.</returns>
        public S3FileInfo MoveTo(S3DirectoryInfo path)
        {
            S3FileInfo ret = CopyTo(path, false);

            Delete();
            return(ret);
        }
示例#5
0
        /// <summary>
        /// Moves the file from the local file system to S3 in this directory.
        /// If the file already exists in S3 than an ArgumentException is thrown.
        /// </summary>
        /// <param name="path">The local file system path where the files are to be moved.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists locally.</exception>
        /// <exception cref="T:System.ArgumentException"></exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo where the file is moved to.</returns>
        public S3FileInfo MoveFromLocal(string path)
        {
            S3FileInfo ret = CopyFromLocal(path, false);

            File.Delete(path);
            return(ret);
        }
示例#6
0
        /// <summary>
        /// Moves the file from the local file system to S3 in this directory.
        /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown.
        /// </summary>
        /// <param name="path">The local file system path where the files are to be moved.</param>
        /// <param name="overwrite">Determines whether the file can be overwritten.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception>
        /// <exception cref="T:System.ArgumentException"></exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo where the file is moved to.</returns>
        public S3FileInfo MoveFromLocal(string path, bool overwrite)
        {
            S3FileInfo ret = CopyFromLocal(path, overwrite);

            File.Delete(path);
            return(ret);
        }
示例#7
0
 /// <summary>
 /// Replaces the destination file with the content of this file and then deletes the orignial file.  If a backupDir is specifed then the content of destination file is 
 /// backup to it.
 /// </summary>
 /// <param name="destDir">Where the contents of this file will be copy to.</param>
 /// <param name="backupDir">If specified the destFile is backup to it.</param>
 /// <exception cref="T:System.ArgumentException"></exception>
 /// <exception cref="T:System.IO.IOException"></exception>
 /// <exception cref="T:System.Net.WebException"></exception>
 /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
 /// <returns>S3FileInfo of the destination file.</returns>
 public S3FileInfo Replace(S3DirectoryInfo destDir, S3DirectoryInfo backupDir)
 {
     S3FileInfo ret = Replace(
         new S3FileInfo(destDir.S3Client, destDir.BucketName, string.Format(CultureInfo.InvariantCulture, "{0}{1}", destDir.ObjectKey, Name)),
         backupDir == null ? null : new S3FileInfo(backupDir.S3Client, backupDir.BucketName, string.Format(CultureInfo.InvariantCulture, "{0}{1}", backupDir.ObjectKey, Name)));
     return ret;
 }
示例#8
0
        /// <summary>
        /// Replaces the destination file with the content of this file and then deletes the orignial file.  If a backupDir is specifed then the content of destination file is
        /// backup to it.
        /// </summary>
        /// <param name="destDir">Where the contents of this file will be copy to.</param>
        /// <param name="backupDir">If specified the destFile is backup to it.</param>
        /// <exception cref="T:System.ArgumentException"></exception>
        /// <exception cref="T:System.IO.IOException"></exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo of the destination file.</returns>
        public S3FileInfo Replace(S3DirectoryInfo destDir, S3DirectoryInfo backupDir)
        {
            S3FileInfo ret = Replace(
                new S3FileInfo(destDir.S3Client, destDir.BucketName, String.Format("{0}{1}", destDir.ObjectKey, Name)),
                backupDir == null ? null : new S3FileInfo(backupDir.S3Client, backupDir.BucketName, String.Format("{0}{1}", backupDir.ObjectKey, Name)));

            return(ret);
        }
 internal S3FileInfo CreateFile(string virtualPath, byte[] contents)
 {
     var fInfo = new S3FileInfo(Client, BucketName, virtualPath);
     using (var stream = fInfo.Create())
     {
         stream.Write(contents, 0, contents.Length);
     }
     return fInfo;
 }
示例#10
0
        /// <summary>
        /// Copies this file to the target directory.
        /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown.
        /// </summary>
        /// <param name="dir">Target directory where to copy the file to.</param>
        /// <param name="overwrite">Determines whether the file can be overwritten.</param>
        /// <exception cref="T:System.ArgumentException">If the directory does not exist.</exception>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo of the newly copied file.</returns>
        public S3FileInfo CopyTo(S3DirectoryInfo dir, bool overwrite)
        {
            if (!dir.Exists)
            {
                throw new ArgumentException("directory does not exist", "dir");
            }

            S3FileInfo ret = CopyTo(new S3FileInfo(dir.S3Client, dir.BucketName, string.Format(CultureInfo.InvariantCulture, "{0}{1}", dir.ObjectKey, Name)),overwrite);
            return ret;
        }
示例#11
0
 /// <summary>
 /// Replaces the destination file with the content of this file and then deletes the orignial file.  If a backupFile is specifed then the content of destination file is 
 /// backup to it.
 /// </summary>
 /// <param name="destFile">Where the contents of this file will be copy to.</param>
 /// <param name="backupFile">If specified the destFile is backup to it.</param>
 /// <exception cref="T:System.ArgumentException"></exception>
 /// <exception cref="T:System.IO.IOException"></exception>
 /// <exception cref="T:System.Net.WebException"></exception>
 /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
 /// <returns>S3FileInfo of the destination file.</returns>
 public S3FileInfo Replace(S3FileInfo destFile, S3FileInfo backupFile)
 {
     if (string.Equals(this.BucketName, destFile.BucketName) && string.Equals(this.ObjectKey, destFile.ObjectKey))
     {
         throw new ArgumentException("Destination file can not be the same as the source file when doing a replace.", "destFile");
     }
     if (backupFile != null)
     {
         destFile.CopyTo(backupFile, true);
     }
     S3FileInfo ret = CopyTo(destFile, true);
     Delete();
     return ret;
 }
示例#12
0
        /// <summary>
        /// Copies this file to the location indicated by the passed in S3FileInfo.
        /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown.
        /// </summary>
        /// <param name="file">The target location to copy this file to.</param>
        /// <param name="overwrite">Determines whether the file can be overwritten.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo of the newly copied file.</returns>
        public S3FileInfo CopyTo(S3FileInfo file, bool overwrite)
        {
            if (!overwrite)
            {
                if (file.Exists)
                {
                    throw new IOException("File already exists");
                }
            }

            if (SameClient(file))
            {
                var request = new CopyObjectRequest
                {
                    DestinationBucket = file.BucketName,
                    DestinationKey    = S3Helper.EncodeKey(file.ObjectKey),
                    SourceBucket      = bucket,
                    SourceKey         = S3Helper.EncodeKey(key)
                };
                ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)request).AddBeforeRequestHandler(S3Helper.FileIORequestEventHandler);
                s3Client.CopyObject(request);
            }
            else
            {
                var getObjectRequest = new GetObjectRequest
                {
                    BucketName = bucket,
                    Key        = S3Helper.EncodeKey(key)
                };
                ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)getObjectRequest).AddBeforeRequestHandler(S3Helper.FileIORequestEventHandler);
                var getObjectResponse = s3Client.GetObject(getObjectRequest);
                using (Stream stream = getObjectResponse.ResponseStream)
                {
                    var putObjectRequest = new PutObjectRequest
                    {
                        BucketName  = file.BucketName,
                        Key         = S3Helper.EncodeKey(file.ObjectKey),
                        InputStream = stream
                    };
                    ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)putObjectRequest).AddBeforeRequestHandler(S3Helper.FileIORequestEventHandler);
                    file.S3Client.PutObject(putObjectRequest);
                }
            }

            return(file);
        }
示例#13
0
        /// <summary>
        /// Replaces the destination file with the content of this file and then deletes the orignial file.  If a backup location is specifed then the content of destination file is
        /// backup to it.
        /// </summary>
        /// <param name="destinationBucket">Destination bucket of this file will be copy to.</param>
        /// <param name="destinationKey">Destination object key of this file will be copy to.</param>
        /// <param name="backupBucket">Backup bucket to store the contents of the destination file.</param>
        /// <param name="backupKey">Backup object key to store the contents of the destination file.</param>
        /// <exception cref="T:System.ArgumentException"></exception>
        /// <exception cref="T:System.IO.IOException"></exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo of the destination file.</returns>
        public S3FileInfo Replace(string destinationBucket, string destinationKey, string backupBucket, string backupKey)
        {
            if (String.IsNullOrEmpty(destinationBucket))
            {
                throw new ArgumentException("A bucket is required to replace an object", "destinationBucket");
            }

            S3FileInfo destinationInfo;

            if (String.IsNullOrEmpty(destinationKey))
            {
                destinationInfo = new S3FileInfo(this.s3Client, destinationBucket, this.Name);
            }
            else if (destinationKey.EndsWith("\\", StringComparison.Ordinal))
            {
                destinationInfo = new S3FileInfo(this.s3Client, destinationBucket, destinationKey + this.Name);
            }
            else
            {
                destinationInfo = new S3FileInfo(this.s3Client, destinationBucket, destinationKey);
            }


            S3FileInfo backupInfo = null;

            if (!string.IsNullOrEmpty(backupBucket))
            {
                if (String.IsNullOrEmpty(backupKey))
                {
                    backupInfo = new S3FileInfo(this.s3Client, backupBucket, this.Name);
                }
                else if (backupKey.EndsWith("\\", StringComparison.Ordinal))
                {
                    backupInfo = new S3FileInfo(this.s3Client, backupBucket, backupKey + this.Name);
                }
                else
                {
                    backupInfo = new S3FileInfo(this.s3Client, backupBucket, backupKey);
                }
            }

            S3FileInfo ret = Replace(destinationInfo, backupInfo);

            return(ret);
        }
示例#14
0
        /// <summary>
        /// Copies this file's content to the file indicated by the S3 bucket and object key.
        /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown.
        /// </summary>
        /// <param name="newBucket">S3 bucket to copy the file to.</param>
        /// <param name="newKey">S3 object key to copy the file to.</param>
        /// <param name="overwrite">Determines whether the file can be overwritten.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception>
        /// <exception cref="T:System.ArgumentException"></exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo of the newly copied file.</returns>
        public S3FileInfo CopyTo(string newBucket, string newKey, bool overwrite)
        {
            if (String.IsNullOrEmpty(newBucket))
            {
                throw new ArgumentException("A bucket is required to copy an object","newBucket");
            }

            S3FileInfo ret = null;
            if (String.IsNullOrEmpty(newKey) || newKey.EndsWith("\\", StringComparison.Ordinal))
            {
                ret = CopyTo(new S3DirectoryInfo(s3Client, newBucket, newKey), overwrite);
            }
            else
            {
                ret = CopyTo(new S3FileInfo(s3Client, newBucket, newKey), overwrite);
            }
            return ret;
        }
示例#15
0
        public static bool DoesObjectExists(string bucketName, string FileName)
        {
            try
            {
                S3FileInfo s3FileInfo = new Amazon.S3.IO.S3FileInfo(s3Client, bucketName, FileName);
                return(s3FileInfo.Exists);
            }
            catch (Amazon.S3.AmazonS3Exception ex)
            {
                if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    return(false);
                }

                //status wasn't not found, so throw the exception
                throw;
            }
        }
示例#16
0
        /// <summary>
        /// The method returns true if the "dataname" item exists
        /// </summary>
        public bool DataItemExists(string bucketname, string dataname)
        {
            // Reset error info
            ClearErrorInfo();

            // Data exists
            bool result = false;

            try
            {
                S3FileInfo s3FileInfo = new Amazon.S3.IO.S3FileInfo(S3client, bucketname, dataname);
                result = s3FileInfo.Exists;
            }
            catch (Exception ex)
            {
                ErrorCode    = -1;
                ErrorMessage = ex.Message;
            }

            return(result);
        }
示例#17
0
        public bool UploadFile(string sourcePath, string fileName)
        {
            var fileExists = new Amazon.S3.IO.S3FileInfo(Client, BucketName, fileName);

            var b = false;

            try
            {
                if (fileExists.Exists)
                {
                    // Delete existing file and re- upload
                    Client.DeleteObject(new Amazon.S3.Model.DeleteObjectRequest()
                    {
                        BucketName = BucketName, Key = fileName
                    });
                }

                var utility = new TransferUtility(Client);
                var request = new TransferUtilityUploadRequest
                {
                    BucketName = BucketName,
                    Key        = fileName,
                    FilePath   = sourcePath
                };

                utility.Upload(request);
                b = true;
            }
            catch (AmazonS3Exception ex)
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
                Console.WriteLine("Response Status Code: " + ex.StatusCode);
                Console.WriteLine("Error Code: " + ex.ErrorCode);
                Console.WriteLine("Error Type: " + ex.ErrorType);
                Console.WriteLine("Request ID: " + ex.RequestId);
            }
            return(b);
        }
示例#18
0
        /// <summary>
        /// Copies this file to the location indicated by the passed in S3FileInfo.
        /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown.
        /// </summary>
        /// <param name="file">The target location to copy this file to.</param>
        /// <param name="overwrite">Determines whether the file can be overwritten.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo of the newly copied file.</returns>
        public S3FileInfo CopyTo(S3FileInfo file, bool overwrite)
        {
            if (!overwrite)
            {
                if (file.Exists)
                {
                    throw new IOException("File already exists");
                }
            }

            if (SameClient(file))
            {
                s3Client.CopyObject(new CopyObjectRequest()
                                    .WithDestinationBucket(file.BucketName)
                                    .WithDestinationKey(S3Helper.EncodeKey(file.ObjectKey))
                                    .WithSourceBucket(bucket)
                                    .WithSourceKey(S3Helper.EncodeKey(key))
                                    .WithBeforeRequestHandler(S3Helper.FileIORequestEventHandler) as CopyObjectRequest);
            }
            else
            {
                using (Stream stream = s3Client.GetObject(new GetObjectRequest()
                                                          .WithBucketName(bucket)
                                                          .WithKey(S3Helper.EncodeKey(key))
                                                          .WithBeforeRequestHandler(S3Helper.FileIORequestEventHandler) as GetObjectRequest)
                                       .ResponseStream)
                {
                    file.S3Client.PutObject((PutObjectRequest) new PutObjectRequest()
                                            .WithBucketName(file.BucketName)
                                            .WithKey(S3Helper.EncodeKey(file.ObjectKey))
                                            .WithInputStream(stream)
                                            .WithBeforeRequestHandler(S3Helper.FileIORequestEventHandler));
                }
            }

            return(file);
        }
示例#19
0
        /// <summary>
        /// Retrieves an URL with the appropriate signature to access the file for a predetermined amount of time
        /// </summary>
        /// <param name="BucketName">S3 bucket name</param>
        /// <param name="Key">S3 object key</param>
        /// <param name="expirationInMinutes">Expiration of link in minutes</param>
        /// <returns>The URL that can be used to access the file</returns>
        public string GetS3SignedUrl(string BucketName, string Key, int expirationInMinutes)
        {
            if (!IsS3Valid)
            {
                return(null);
            }

            string url = null;

            S3FileInfo s3FileInfo = new Amazon.S3.IO.S3FileInfo(S3Client, BucketName, Key);

            if (s3FileInfo.Exists)
            {
                url = S3Client.GetPreSignedURL(new GetPreSignedUrlRequest
                {
                    BucketName = BucketName,
                    Key        = Key,
                    Expires    = DateTime.UtcNow.AddMinutes(expirationInMinutes)
                                 //ResponseHeaderOverrides = new ResponseHeaderOverrides() { ContentDisposition = "attachment; filename=\"test.txt\"", }
                });
            }

            return(url);
        }
 public void DeleteFile(string path)
 {
     path = CleanPath(path);
     var file = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, path);
     file.Delete();
     
 }
 public void RenameFile(string oldPath, string newPath)
 {
     oldPath = CleanPath(oldPath);
     newPath = CleanPath(newPath);
     var file = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, oldPath);
     file.MoveToLocal(newPath);
     
 }
 public AmazonS3StreamProxy(Stream stream, IAmazonS3StorageProvider provider, S3FileInfo fileInfo)
 {
     _stream = stream;
     _provider = provider;
     _fileInfo = fileInfo;
 }
示例#23
0
        internal S3FileStream(AmazonS3 s3Client, string bucket, string key, FileMode mode, FileAccess access, int buffersize)
        {
            file = new S3FileInfo(s3Client, bucket, key);
            buffer = new MemoryStream(buffersize);
            this.mode = mode;
            this.access = access;

            fileExist = file.ExistsWithBucketCheck(out bucketExist);

            if ((access & FileAccess.Read) != FileAccess.Read)
            {
                canRead = false;
            }
            if ((access & FileAccess.Write) != FileAccess.Write)
            {
                canWrite = false;
            }

            switch (mode)
            {
                case FileMode.Append:
                    if ((access & FileAccess.Write) != FileAccess.Write)
                    {
                        throw new ArgumentException("Append requires Write access");
                    }
                    PopulateData();
                    buffer.Seek(0, SeekOrigin.End);
                    startPosition = buffer.Position;
                    break;

                case FileMode.Create:
                    if ((access & FileAccess.Write) != FileAccess.Write)
                    {
                        throw new ArgumentException("Create requires Write access");
                    }
                    break;

                case FileMode.CreateNew:
                    if (fileExist)
                    {
                        throw new IOException("CreateNew requires the file not to already exist");
                    }
                    if ((access & FileAccess.Write) != FileAccess.Write)
                    {
                        throw new ArgumentException("Create requires Write access");
                    }
                    break;

                case FileMode.Open:
                    if (!fileExist)
                    {
                        throw new IOException("Open requires the file to already exist");
                    }
                    PopulateData();
                    break;

                case FileMode.OpenOrCreate:
                    if (fileExist)
                    {
                        if ((access & FileAccess.Write) != FileAccess.Write)
                        {
                            throw new ArgumentException("Create requires Write access");
                        }
                    }
                    break;

                case FileMode.Truncate:
                    if (!fileExist)
                    {
                        throw new IOException("Truncate requires the file to already exist");
                    }
                    if ((access & FileAccess.Write) != FileAccess.Write)
                    {
                        throw new ArgumentException("Truncate requires Write access");
                    }
                    break;

                default:
                    throw new ArgumentException("Invalid value", "mode");
            }
        }
        /// <summary>
        /// Retrieves a file within the storage provider.
        /// </summary>
        /// <param name="path">The relative path to the file within the storage provider.</param>
        /// <returns>The file.</returns>
        /// <exception cref="ArgumentException">If the file is not found.</exception>
        public IStorageFile GetFile(string path) {
            EnsureInitialized();
            if (_client == null) return null;
            // seperate folder form file
                var request = new GetObjectRequest();
                request.BucketName = BucketName;
                request.Key = path;
                request.ResponseExpires = DateTime.Now.AddMinutes(5);

                using (GetObjectResponse response = _client.GetObject(request)) {
                    var fileInfo = new S3FileInfo(_client, BucketName, response.Key);
                    return new S3StorageFile(fileInfo, this);

                }

                //using (GetObjectResponse response = client.GetObject(request))
                //{
                //    response.Key.Substring()
                //   foreach (var entry in response.S3Objects.Where(e => e.Key == path))
                //   {
                //       var mimeType = AmazonS3Util.MimeTypeFromExtension(entry.Key.Substring(entry.Key.LastIndexOf(".", System.StringComparison.Ordinal)));
                //       return new S3StorageFile(entry, mimeType);
                //   }
                //}



        }
示例#25
0
 public S3VirtualFile(IVirtualPathProvider owningProvider, IVirtualDirectory directory, S3FileInfo fInfo) 
     : base(owningProvider, directory)
 {
     this.BackingFile = fInfo;
 }
 public IStorageFile GetFile(string path)
 {
     path = CleanPath(path);
     var file = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, path);
     return new AmazonS3StorageFile(file, this);
 }
示例#27
0
 private bool SameClient(S3FileInfo otherFile)
 {
     return s3Client.Equals(otherFile.S3Client);
 }
示例#28
0
        /// <summary>
        /// Replaces the destination file with the content of this file and then deletes the orignial file.  If a backup location is specifed then the content of destination file is 
        /// backup to it.
        /// </summary>
        /// <param name="destinationBucket">Destination bucket of this file will be copy to.</param>
        /// <param name="destinationKey">Destination object key of this file will be copy to.</param>
        /// <param name="backupBucket">Backup bucket to store the contents of the destination file.</param>
        /// <param name="backupKey">Backup object key to store the contents of the destination file.</param>
        /// <exception cref="T:System.ArgumentException"></exception>
        /// <exception cref="T:System.IO.IOException"></exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo of the destination file.</returns>
        public S3FileInfo Replace(string destinationBucket, string destinationKey, string backupBucket, string backupKey)
        {
            if (String.IsNullOrEmpty(destinationBucket))
            {
                throw new ArgumentException("A bucket is required to replace an object", "destinationBucket");
            }

            S3FileInfo destinationInfo;
            if (String.IsNullOrEmpty(destinationKey))
            {
                destinationInfo = new S3FileInfo(this.s3Client, destinationBucket, this.Name);
            }
            else if (destinationKey.EndsWith("\\", StringComparison.Ordinal))
            {
                destinationInfo = new S3FileInfo(this.s3Client, destinationBucket, destinationKey + this.Name);
            }
            else
            {
                destinationInfo = new S3FileInfo(this.s3Client, destinationBucket, destinationKey);
            }


            S3FileInfo backupInfo = null;
            if(!string.IsNullOrEmpty(backupBucket))
            {
                if (String.IsNullOrEmpty(backupKey))
                {
                    backupInfo = new S3FileInfo(this.s3Client, backupBucket, this.Name);
                }
                else if (backupKey.EndsWith("\\", StringComparison.Ordinal))
                {
                    backupInfo = new S3FileInfo(this.s3Client, backupBucket, backupKey + this.Name);
                }
                else 
                {
                    backupInfo = new S3FileInfo(this.s3Client, backupBucket, backupKey);
                }        
            }

            S3FileInfo ret = Replace(destinationInfo, backupInfo);
            return ret;
        }
        public Stream GetObjectStream(string path)
        {
            path = CleanPath(path);
            var file = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, path);

            return file.OpenRead();
            //return Download(path);
        }
        public override bool Exists(FileSystemObject fso)
        {
            var bki = new S3BucketKeyInfo(fso.FullName);

            var s3FileInfo = new S3FileInfo(S3Client, bki.BucketName, bki.Key);

            return s3FileInfo.Exists;
        }
示例#31
0
        /// <summary>
        /// Copies this file to the location indicated by the passed in S3FileInfo.
        /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown.
        /// </summary>
        /// <param name="file">The target location to copy this file to.</param>
        /// <param name="overwrite">Determines whether the file can be overwritten.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo of the newly copied file.</returns>
        public S3FileInfo CopyTo(S3FileInfo file, bool overwrite)
        {
            if (!overwrite)
            {
                if (file.Exists)
                {
                    throw new IOException("File already exists");
                }
            }

            if (SameClient(file))
            {
                s3Client.CopyObject(new CopyObjectRequest()
                    .WithDestinationBucket(file.BucketName)
                    .WithDestinationKey(S3Helper.EncodeKey(file.ObjectKey))
                    .WithSourceBucket(bucket)
                    .WithSourceKey(S3Helper.EncodeKey(key))
                    .WithBeforeRequestHandler(S3Helper.FileIORequestEventHandler) as CopyObjectRequest);
            }
            else
            {
                using (Stream stream = s3Client.GetObject(new GetObjectRequest()
                                .WithBucketName(bucket)
                                .WithKey(S3Helper.EncodeKey(key))
                                .WithBeforeRequestHandler(S3Helper.FileIORequestEventHandler) as GetObjectRequest)
                            .ResponseStream)
                {
                    file.S3Client.PutObject((PutObjectRequest)new PutObjectRequest()
                        .WithBucketName(file.BucketName)
                        .WithKey(S3Helper.EncodeKey(file.ObjectKey))
                        .WithInputStream(stream)
                        .WithBeforeRequestHandler(S3Helper.FileIORequestEventHandler));
                }
            }

            return file;
        }
 public bool FileExists(string path)
 {
     path = CleanPath(path);
     var file = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, path);
     return file.Exists;
 }
示例#33
0
 private bool SameClient(S3FileInfo otherFile)
 {
     return(s3Client.Equals(otherFile.S3Client));
 }
 public S3StorageFile(S3FileInfo s3FileInfo, S3StorageProvider s3StorageProvider) {
     _s3FileInfo = s3FileInfo;
     _s3StorageProvider = s3StorageProvider;
 }
 internal System.IO.Stream CreateFile(string virtualPath)
 {
     var fInfo = new S3FileInfo(Client, BucketName, virtualPath);
     return fInfo.Create();
 }
示例#36
0
        public vEntityRespuestaCargaNube CargarArchivo(string ruta, string checkSum, string bucket, string key)
        {
            vEntityRespuestaCargaNube resultado = null;

            AmazonObjectUrl urlAmazon = new AmazonObjectUrl()
            {
                Bucket = bucket,
                Key    = key
            };

            try
            {
                Amazon.Runtime.AWSCredentials credentials = new Amazon.Runtime.StoredProfileAWSCredentials(Constants.PerfilSoportesSDKStore);

                using (IAmazonS3 s3Client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.GetBySystemName(Constants.RegionDefecto)))
                {
                    string tagCheckSum = "x-amz-meta-checksum";

                    var fileInfo = new Amazon.S3.IO.S3FileInfo(s3Client, urlAmazon.Bucket, urlAmazon.Key);
                    if (fileInfo.Exists)
                    {
                        var respGet = s3Client.GetObjectMetadata(urlAmazon.Bucket, urlAmazon.Key);
                        if (respGet.Metadata[tagCheckSum] == checkSum)
                        {
                            resultado = new vEntityRespuestaCargaNube()
                            {
                                Exito = true,
                                Link  = urlAmazon.ToString()
                            };
                        }
                        else
                        {
                            resultado = new vEntityRespuestaCargaNube()
                            {
                                Exito   = false,
                                Link    = urlAmazon.ToString(),
                                Mensaje = "Hay un archivo con la misma ruta en el servidor, pero tienen checksum diferente"
                            };
                        }
                    }

                    if (resultado == null)
                    {
                        using (var reader = new System.IO.StreamReader(ruta))
                        {
                            var request = new PutObjectRequest()
                            {
                                BucketName  = urlAmazon.Bucket,
                                Key         = urlAmazon.Key,
                                CannedACL   = S3CannedACL.Private,
                                InputStream = reader.BaseStream
                            };
                            request.Metadata.Add(tagCheckSum, checkSum);

                            var respIns = s3Client.PutObject(request);

                            var fileInfo2 = new Amazon.S3.IO.S3FileInfo(s3Client, urlAmazon.Bucket, urlAmazon.Key);
                            if (fileInfo2.Exists)
                            {
                                resultado = new vEntityRespuestaCargaNube()
                                {
                                    Exito = true,
                                    Link  = urlAmazon.ToString()
                                };
                            }
                            else
                            {
                                resultado = new vEntityRespuestaCargaNube()
                                {
                                    Exito   = false,
                                    Link    = urlAmazon.ToString(),
                                    Mensaje = "Se ejecuto el proceso de cargue del archivo, pero no se pudo confirmar su existencia en la nube."
                                };
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                resultado = new vEntityRespuestaCargaNube()
                {
                    Exito   = false,
                    Link    = urlAmazon.ToString(),
                    Mensaje = String.Format("Error al cargar archivo a Amazon. Excepción: {0}", ex.ToString())
                };
            }

            return(resultado);
        }
示例#37
0
        public static UploadedFile UploadToS3Images(HttpPostedFileBase file, int eventId)
        {
            var uploadedFile = new UploadedFile();
               // string fileName = file.FileName.Replace(" ", "+");
            string fileName = Regex.Replace(file.FileName, @"\s+", "");
            try
            {
                IAmazonS3 client;
                using (client = GetAmazonClient())
                {
                    string fileUploadKey = Convert.ToString(eventId) + "/";
                    var s3FileInfo = new S3FileInfo(client, AppSettingHelper.GetAmazonBucketName(),
                        fileUploadKey + fileName);
                    if (s3FileInfo.Exists)
                    {
                        // If file exists
                        string strRandomFileName = Path.GetRandomFileName();

                        //This method returns a random file name of 11 characters
                        string fileExtension = Path.GetExtension(fileName);
                        strRandomFileName = strRandomFileName.Replace(".", "") + fileExtension;

                        #region Code For if file exist on amazon for a particular EventId.

                        fileUploadKey = Convert.ToString(eventId) + "/" + strRandomFileName;
                        fileName = strRandomFileName;

                        #endregion
                    }
                    else
                    {
                        #region Code For if File does not exist in event bucket on Amazon

                        fileUploadKey = Convert.ToString(eventId) + "/" + fileName;

                        #endregion
                    }

                    PutObjectRequest request = GetAmazonObjectRequest(fileUploadKey);
                    request.InputStream = file.InputStream;
                    PutObjectResponse response = client.PutObject(request);

                    if (response.HttpStatusCode.ToString() == "OK")
                    {
                       #region Construct model for Uploaded video file

                        uploadedFile.FileName = fileName;
                        uploadedFile.FileUrl = AppSettingHelper.GetAmazonFileUrl() +
                                               AppSettingHelper.GetAmazonBucketName() + "/" + fileUploadKey;
                        uploadedFile.UploadStatus = true;

                        #endregion
                    }
                }
                return uploadedFile;
            }
            catch (Exception ex)
            {
                return uploadedFile;
            }
        }
示例#38
0
        /// <summary>
        /// Copies this file to the location indicated by the passed in S3FileInfo.
        /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown.
        /// </summary>
        /// <param name="file">The target location to copy this file to.</param>
        /// <param name="overwrite">Determines whether the file can be overwritten.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
        /// <returns>S3FileInfo of the newly copied file.</returns>
        public S3FileInfo CopyTo(S3FileInfo file, bool overwrite)
        {
            if (!overwrite)
            {
                if (file.Exists)
                {
                    throw new IOException("File already exists");
                }
            }

            if (SameClient(file))
            {
                var request = new CopyObjectRequest
                {
                    DestinationBucket = file.BucketName,
                    DestinationKey = S3Helper.EncodeKey(file.ObjectKey),
                    SourceBucket = bucket,
                    SourceKey = S3Helper.EncodeKey(key)
                };
                request.BeforeRequestEvent += S3Helper.FileIORequestEventHandler;
                s3Client.CopyObject(request);
            }
            else
            {
                var getObjectRequest = new GetObjectRequest
                {
                    BucketName = bucket,
                    Key = S3Helper.EncodeKey(key)
                };
                getObjectRequest.BeforeRequestEvent += S3Helper.FileIORequestEventHandler;
                var getObjectResponse = s3Client.GetObject(getObjectRequest);
                using (Stream stream = getObjectResponse.ResponseStream)
                {
                    var putObjectRequest = new PutObjectRequest
                    {
                        BucketName = file.BucketName,
                        Key = S3Helper.EncodeKey(file.ObjectKey),
                        InputStream = stream
                    };
                    putObjectRequest.BeforeRequestEvent += S3Helper.FileIORequestEventHandler;
                    file.S3Client.PutObject(putObjectRequest);
                }
            }

            return file;
        }
        public IStorageFile CreateFile(string path)
        {
            //throw new NotImplementedException("File creation currently not supported.");
            Logger.Information("CreateFile");
            PutObjectRequest request = new PutObjectRequest
            {

                BucketName = BucketName,
                Key = path,
                CannedACL = S3CannedACL.PublicRead,
                InputStream = new MemoryStream(),
                Timeout = TimeSpan.FromSeconds(300),
                ReadWriteTimeout = TimeSpan.FromMinutes(5)
            };

            //request.WithBucketName().WithKey(path).WithCannedACL(S3CannedACL.PublicRead).WithInputStream(inputStream);

            // add far distance experiy date
            request.Headers["Expires"] = DateTime.Now.AddYears(10).ToString("ddd, dd, MMM yyyy hh:mm:ss") + " GMT";
            request.Headers["x-amz-acl"] = "public-read";

            var response = _client.PutObject(request);

            var fileInfo = new S3FileInfo(_client, BucketName, path);
            return new S3StorageFile(fileInfo, this);
        }
 public AmazonS3StorageFile(S3FileInfo s3FileInfo, IAmazonS3StorageProvider storageProvider)
 {
     _s3FileInfo = s3FileInfo;
     _storageProvider = storageProvider;
 }
 public void CopyFile(string originalPath, string duplicatePath)
 {
     originalPath = CleanPath(originalPath);
     duplicatePath = CleanPath(duplicatePath);
     var file = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, originalPath);
     file.CopyToLocal(duplicatePath);
 }
示例#42
0
        public AWSStorageFile(S3FileInfo fileInfo)
        {

        }
示例#43
0
 /// <summary>
 /// Copies this file to the location indicated by the passed in S3FileInfo.
 /// If the file already exists in S3 than an ArgumentException is thrown.
 /// </summary>
 /// <param name="file">The target location to copy this file to.</param>
 /// <exception cref="T:System.IO.IOException">If the file already exists in S3.</exception>
 /// <exception cref="T:System.Net.WebException"></exception>
 /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
 /// <returns>S3FileInfo of the newly copied file.</returns>
 public S3FileInfo CopyTo(S3FileInfo file)
 {
     return CopyTo(file, false);
 }
 public void SaveStream(string path, Stream inputStream)
 {
     path = CleanPath(path);
     var file = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, path);
     var isNew = !file.Exists;
     using (var stream = file.Exists ? file.OpenWrite() : file.Create())
     {
         inputStream.CopyTo(stream);
     }
     if (isNew)
     {
         PublishFile(path);
     }
 }
示例#45
0
 /// <summary>
 /// Moves the file to a a new location in S3.
 /// </summary>
 /// <param name="file">The target file to copy to.</param>
 /// <exception cref="T:System.IO.IOException">If the file already exists in S3.</exception>
 /// <exception cref="T:System.ArgumentException"></exception>
 /// <exception cref="T:System.Net.WebException"></exception>
 /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
 /// <returns>S3FileInfo for the target location.</returns>
 public S3FileInfo MoveTo(S3FileInfo file)
 {
     S3FileInfo ret = CopyTo(file, false);
     Delete();
     return ret;
 }
 public IStorageFile CreateFile(string path)
 {
     path = CleanPath(path);
     var file = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, path);
     using (file.Create()) { }
     PublishFile(path);
     return new AmazonS3StorageFile(file, this);
 }
示例#47
0
 /// <summary>
 /// Replaces the destination file with the content of this file and then deletes the orignial file.  If a backupFile is specifed then the content of destination file is 
 /// backup to it.
 /// </summary>
 /// <param name="destFile">Where the contents of this file will be copy to.</param>
 /// <param name="backupFile">If specified the destFile is backup to it.</param>
 /// <exception cref="T:System.ArgumentException"></exception>
 /// <exception cref="T:System.IO.IOException"></exception>
 /// <exception cref="T:System.Net.WebException"></exception>
 /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
 /// <returns>S3FileInfo of the destination file.</returns>
 public S3FileInfo Replace(S3FileInfo destFile, S3FileInfo backupFile)
 {
     if (string.Equals(this.BucketName, destFile.BucketName) && string.Equals(this.ObjectKey, destFile.ObjectKey))
     {
         throw new ArgumentException("Destination file can not be the same as the source file when doing a replace.", "destFile");
     }
     if (backupFile != null)
     {
         destFile.CopyTo(backupFile, true);
     }
     S3FileInfo ret = CopyTo(destFile, true);
     Delete();
     return ret;
 }
示例#48
0
 /// <summary>
 /// Copies this file to the location indicated by the passed in S3FileInfo.
 /// If the file already exists in S3 than an ArgumentException is thrown.
 /// </summary>
 /// <param name="file">The target location to copy this file to.</param>
 /// <exception cref="T:System.IO.IOException">If the file already exists in S3.</exception>
 /// <exception cref="T:System.Net.WebException"></exception>
 /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception>
 /// <returns>S3FileInfo of the newly copied file.</returns>
 public S3FileInfo CopyTo(S3FileInfo file)
 {
     return(CopyTo(file, false));
 }
示例#49
0
        internal S3FileStream(AmazonS3 s3Client, string bucket, string key, FileMode mode, FileAccess access, int buffersize)
        {
            file        = new S3FileInfo(s3Client, bucket, key);
            buffer      = new MemoryStream(buffersize);
            this.mode   = mode;
            this.access = access;

            fileExist = file.ExistsWithBucketCheck(out bucketExist);

            if ((access & FileAccess.Read) != FileAccess.Read)
            {
                canRead = false;
            }
            if ((access & FileAccess.Write) != FileAccess.Write)
            {
                canWrite = false;
            }

            switch (mode)
            {
            case FileMode.Append:
                if ((access & FileAccess.Write) != FileAccess.Write)
                {
                    throw new ArgumentException("Append requires Write access");
                }
                PopulateData();
                buffer.Seek(0, SeekOrigin.End);
                startPosition = buffer.Position;
                break;

            case FileMode.Create:
                if ((access & FileAccess.Write) != FileAccess.Write)
                {
                    throw new ArgumentException("Create requires Write access");
                }
                break;

            case FileMode.CreateNew:
                if (fileExist)
                {
                    throw new IOException("CreateNew requires the file not to already exist");
                }
                if ((access & FileAccess.Write) != FileAccess.Write)
                {
                    throw new ArgumentException("Create requires Write access");
                }
                break;

            case FileMode.Open:
                if (!fileExist)
                {
                    throw new IOException("Open requires the file to already exist");
                }
                PopulateData();
                break;

            case FileMode.OpenOrCreate:
                if (fileExist)
                {
                    if ((access & FileAccess.Write) != FileAccess.Write)
                    {
                        throw new ArgumentException("Create requires Write access");
                    }
                }
                break;

            case FileMode.Truncate:
                if (!fileExist)
                {
                    throw new IOException("Truncate requires the file to already exist");
                }
                if ((access & FileAccess.Write) != FileAccess.Write)
                {
                    throw new ArgumentException("Truncate requires Write access");
                }
                break;

            default:
                throw new ArgumentException("Invalid value", "mode");
            }
        }
示例#50
0
        /// <summary>
        /// The method returns true if the "dataname" item exists
        /// </summary>
        public bool DataItemExists(string bucketname, string dataname)
        {
            // Reset error info
            ClearErrorInfo();

            // Data exists
            bool result = false;
            try
            {
                S3FileInfo s3FileInfo = new Amazon.S3.IO.S3FileInfo(S3client, bucketname, dataname);
                result = s3FileInfo.Exists;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorMessage = ex.Message;
            }

            return result;
        }