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);
            }
        }
Exemple #2
0
        /// <summary>
        /// Implementation of the ZephyrFile Create method in Amazon S3 Storage.
        /// </summary>
        /// <param name="overwrite">Will overwrite the file if it already exists.</param>
        /// <param name="callbackLabel">Optional "label" to be passed into the callback method.</param>
        /// <param name="callback">Optional method that is called for logging purposes.</param>
        /// <returns>An instance of a AmazonS3ZephyrFile.</returns>
        public override ZephyrFile Create(bool overwrite = true, bool verbose = true, string callbackLabel = null, Action <string, string> callback = null)
        {
            try
            {
                if (this.Exists && !overwrite)
                {
                    throw new Exception($"File [{this.FullName}] Already Exists.");
                }

                if (_client == null)
                {
                    throw new Exception($"AWSClient Not Set.");
                }

                S3FileInfo fileInfo = new S3FileInfo(_client.Client, BucketName, ObjectKey);
                this.Stream = fileInfo.Create();
                // File isn't written to S3 Bucket Until The Stream Is Closed.  Force Write By Closing Stream.
                this.Close(false);
                this.Open(AccessType.Write, false);
                if (verbose)
                {
                    Logger.Log($"File [{FullName}] Was Created.", callbackLabel, callback);
                }
                return(this);
            }
            catch (Exception e)
            {
                Logger.Log($"ERROR - {e.Message}", callbackLabel, callback);
                throw;
            }
        }
        /// <summary>
        /// Opens the specified file for reading or writing.
        /// </summary>
        /// <param name="path">The path of the file to open.</param>
        /// <param name="mode">Specified if the file should be opened, created, overwritten or truncated.</param>
        /// <param name="access">Specified if the stream should be opened for reading or writing.</param>
        /// <returns>A <see cref="T:Systen.IO.Stream"/> that can be used to read or write the content of the file. </returns>
        public Stream OpenFileStream(string path, FileMode mode, FileAccess access)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            path = MapPath(path);
            path = path.Substring(this.BucketName.Length + 1);
            if (mode == FileMode.Open && access == FileAccess.Read)
            {
                var info = new S3FileInfo(S3, this.BucketName, path);
                return(info.OpenRead());
            }
            else if (mode == FileMode.Open && access == FileAccess.Write)
            {
                var info = new S3FileInfo(S3, this.BucketName, path);
                return(info.OpenWrite());
            }
            else if (mode == FileMode.Create || mode == FileMode.CreateNew && access == FileAccess.Write)
            {
                var info = new S3FileInfo(S3, this.BucketName, path);
                return(info.Create());
            }

            throw new NotSupportedException();
        }
Exemple #4
0
        public Stream GetObjectStream(string bucketName, string objectKey, S3FileMode fileMode = S3FileMode.Open, S3FileAccess fileAccess = S3FileAccess.Read)
        {
            Stream     stream     = null;
            S3FileInfo file       = new S3FileInfo(client, bucketName, objectKey);
            bool       fileExists = file.Exists;

            if (fileMode == S3FileMode.Create || fileMode == S3FileMode.CreateNew || fileMode == S3FileMode.Truncate)
            {
                if (fileExists)
                {
                    if (fileMode == S3FileMode.CreateNew)
                    {
                        throw new Exception($"Object [s3://{bucketName}/{objectKey}] Already Exists.");
                    }
                    file.Delete();
                }
                else if (fileMode == S3FileMode.Truncate)
                {
                    throw new Exception($"Object [s3://{bucketName}/{objectKey}] Does Not Exist.");
                }

                stream = file.Create();
            }
            else if (fileMode == S3FileMode.Open || fileMode == S3FileMode.OpenOrCreate)
            {
                if (!fileExists)
                {
                    if (fileMode == S3FileMode.Open)
                    {
                        throw new Exception($"Object [s3://{bucketName}/{objectKey}] Does Not Exist.");
                    }
                    stream = file.Create();
                    stream.Close();
                }

                if (fileAccess == S3FileAccess.Read)
                {
                    stream = file.OpenRead();
                }
                else
                {
                    stream = file.OpenWrite();
                }
            }

            return(stream);
        }
        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));
        }
        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);
        }
 public Stream CreateFile()
 {
     if (_s3FileInfo.Exists)
     {
         return(OpenWrite());
     }
     using (var stream = _s3FileInfo.Create()) {};
     _storageProvider.PublishFile(_s3FileInfo.FullName);
     return(_s3FileInfo.OpenWrite());
 }
        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);
            }
        }
Exemple #9
0
        public IStorageFile CreateFile(string path, byte[] arr = null)
        {
            if (IsFileExists(path))
            {
                throw new InvalidOperationException("File " + path + " already exists");
            }

            var fileInfo = new S3FileInfo(_amazonS3, _bucketName, path);

            using (var stream = fileInfo.Create())
            {
                stream.Write(new byte[0], 0, 0);
            }

            return(new AmazonStorageFile(fileInfo));
        }
Exemple #10
0
        public void SaveStream(string path, Stream inputStream, string bucketName)
        {
            path = CleanPath(path);
            var file  = new S3FileInfo(_client, bucketName, path);
            var isNew = !file.Exists;

            using (var stream = file.Exists ? file.OpenWrite() : file.Create())
            {
                inputStream.CopyTo(stream);
                inputStream.Close();
            }
            if (isNew)
            {
                PublishFile(path);
            }
        }
        public string SaveFile(FileDescription fileDescription, Stream stream, SaveOptions saveOptions)
        {
            string key = GetOutputPath(fileDescription, saveOptions);

            stream.Seek(0, SeekOrigin.Begin);
            S3FileInfo fileInfo = new S3FileInfo(_client, bucketName, key);

            byte[] buffer = new byte[16384]; //16*1024
            using (Stream output = fileInfo.Create())
            {
                int read = stream.Read(buffer, 0, buffer.Length);
                while (read > 0)
                {
                    output.Write(buffer, 0, read);
                    read = stream.Read(buffer, 0, buffer.Length);
                }
                output.Flush();
                output.Close();
            }
            return(fileInfo.FullName);
        }
Exemple #12
0
        public Stream GetOutputSaveStream(CacheFileDescription cacheFileDescription)
        {
            try
            {
                if (!_conversionConfig.IsUseCache())
                {
                    return(new MemoryStream());
                }

                if (cacheFileDescription == null || String.IsNullOrEmpty(cacheFileDescription.Guid))
                {
                    throw new System.Exception("CacheFileDescription is not set");
                }

                string     key      = GetCachePath(_conversionConfig.CachePath, cacheFileDescription);
                S3FileInfo fileInfo = new S3FileInfo(_client, bucketName, key);
                return(fileInfo.Create());
            }
            catch (System.Exception e)
            {
                throw new System.Exception(e.Message);
            }
        }
        internal System.IO.Stream CreateFile(string virtualPath)
        {
            var fInfo = new S3FileInfo(Client, BucketName, virtualPath);

            return(fInfo.Create());
        }
 public Stream CreateFile()
 {
     return(_s3FileInfo.Create());
 }