Example #1
0
        public static void Move(string oldFileKey, string newFileKey, string folderName, bool overWrite = false)
        {
            if (!IsUseS3)
            {
                return;
            }

            if (string.IsNullOrEmpty(oldFileKey))
            {
                return;
            }

            if (overWrite)
            {
                Delete(newFileKey);
            }

            using (IAmazonS3 client = new AmazonS3Client(Region))
            {
                S3FileInfo      currentObject = new S3FileInfo(client, Bucket, oldFileKey);
                S3DirectoryInfo destination   = new S3DirectoryInfo(client, Bucket, folderName);
                if (!destination.Exists)
                {
                    destination.Create();
                }
                S3FileInfo movedObject = currentObject.MoveTo(Bucket, newFileKey);
            }
            MakePublic(newFileKey);
        }
        public void RenameFile(string oldPath, string newPath)
        {
            oldPath = CleanPath(oldPath);
            newPath = CleanPath(newPath);
            var file    = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, oldPath);
            var newFile = new S3FileInfo(_client, _amazonS3StorageConfiguration.AWSFileBucket, newPath);

            file.MoveTo(newFile);
            PublishFile(newPath);
        }
Example #3
0
        public void RenameFile(string path, string newPath)
        {
            if (!IsFileExists(path))
            {
                throw new InvalidOperationException("File " + path + " does not exist");
            }

            if (IsFileExists(newPath))
            {
                throw new InvalidOperationException("File " + newPath + " already exists");
            }

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

            fileInfo.MoveTo(newFileInfo);
        }
        /// <summary>
        /// Renames the specified directory.
        /// </summary>
        /// <param name="path">Path of the directory to rename.</param>
        /// <param name="newName">The new directory name.</param>
        public void RenameDirectory(string path, string newName)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (newName == null)
            {
                throw new ArgumentNullException("newName");
            }

            path = MapPath(path);
            path = path.Substring(this.BucketName.Length + 1) + "/";
            var newPath = Path.GetDirectoryName(path) + "/" + newName + "/";

            var info = new S3FileInfo(S3, this.BucketName, path);

            info.MoveTo(this.BucketName, newPath);
        }
Example #5
0
        private static void CopyOrMoveFiles(IAmazonS3 client, S3DirectoryInfo origin, S3DirectoryInfo target, bool moveFiles, S3FileInfo file)
        {
            string currentFile   = file.FullName.Replace($"{origin.Bucket.Name}:\\", string.Empty);
            string originFolder  = currentFile.Split('\\').First();
            string targetFolder  = target.FullName.Replace($"{target.Bucket.Name}:\\", string.Empty).Split('\\').First();
            string newFile       = currentFile.Replace(originFolder, targetFolder);
            string verbOperation = moveFiles ? "moved" : "copied";

            S3FileInfo fileOrigin = new S3FileInfo(client, origin.Bucket.Name, currentFile);

            if (!moveFiles)
            {
                fileOrigin.CopyTo(new S3FileInfo(client, target.Bucket.Name, newFile), true);
            }
            else
            {
                fileOrigin.MoveTo(new S3FileInfo(client, target.Bucket.Name, newFile));
            }

            Console.WriteLine(string.Format("File {0} {1} to {2}", fileOrigin.FullName, verbOperation, newFile));
        }
Example #6
0
        static async Task WriteS3()
        {
            try
            {
                AmazonS3Config cfg = new AmazonS3Config
                {
                    RegionEndpoint = Amazon.RegionEndpoint.USEast2 //bucket location
                };


                //var bucketRegion = RegionEndpoint.GetBySystemName("us-east-2");
                var client = new AmazonS3Client(cfg);

                S3FileInfo source = new S3FileInfo(client, bucketName, "to-do/13839648_1.jpg");
                //string bucketName2 = "destination butcket";
                S3FileInfo destination = new S3FileInfo(client, bucketName, "completed/13839648_1.jpg");
                source.MoveTo(destination);
                // 1. Put object-specify only key name for the new object.
                var putRequest1 = new PutObjectRequest
                {
                    BucketName  = bucketName,
                    Key         = "test_sample.txt",
                    ContentBody = "sample text"
                };
                PutObjectResponse response1 = await client.PutObjectAsync(putRequest1);
            }
            catch (AmazonS3Exception e)
            {
                logger.Error(e,
                             "Error encountered ***. when writing an object");
            }
            catch (Exception e)
            {
                logger.Error(e,
                             "Unknown encountered on server. when writing an object");
            }
        }
Example #7
0
        public void Rename(string newName)
        {
            var newKey = _key.Replace(Name, newName);

            _s3FileInfo = _s3FileInfo.MoveTo(_bucketName, newKey);
        }