Ejemplo n.º 1
0
        /// <summary>
        /// Filemask, eg. *.*, *file?.txt.
        /// Bucketname without s3://-prefix.
        /// </summary>
        /// <param name="input" />
        /// <param name="parameters" />
        /// <param name="options" />
        /// <param name="cancellationToken" />
        /// <returns>List&lt;string&gt;</returns>
        public static async Task <List <string> > UploadFiles(
            [PropertyTab] UploadInput input,
            [PropertyTab] Parameters parameters,
            [PropertyTab] UploadOptions options,
            CancellationToken cancellationToken
            )
        {
            if (!parameters.UseDefaultCredentials && parameters.AwsCredentials == null)
            {
                parameters.IsAnyNullOrWhiteSpaceThrow();
            }

            if (!Directory.Exists(input.FilePath))
            {
                throw new ArgumentException(@"Source path not found. ", nameof(input.FilePath));
            }

            var localRoot = new DirectoryInfo(input.FilePath);

            // If filemask is not set, get all files.
            var filesToCopy = localRoot.GetFiles(
                input.FileMask ?? "*",
                options.UploadFromCurrentDirectoryOnly
                    ? SearchOption.TopDirectoryOnly
                    : SearchOption.AllDirectories);

            if (options.ThrowErrorIfNoMatch && filesToCopy.Length < 1)
            {
                throw new ArgumentException($"No files match the filemask within supplied path. {nameof(input.FileMask)}");
            }

            return(await ExecuteUpload(filesToCopy, input.S3Directory, parameters, options, cancellationToken, input));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Filemask is Windows-style, eg. *.*, *file?.txt
        ///     Bucketname without s3://-prefix.
        /// </summary>
        /// <param name="input" />
        /// <param name="parameters" />
        /// <param name="options" />
        /// <param name="cancellationToken" />
        /// <returns>List&lt;string&gt;</returns>
        public static List <string> UploadFiles(
            [PropertyTab] UploadInput input,
            [PropertyTab] Parameters parameters,
            [PropertyTab] UploadOptions options,
            CancellationToken cancellationToken
            )
        {
            // First check to see if this task gets performed at all.
            cancellationToken.ThrowIfCancellationRequested();

            if (parameters.AwsCredentials == null)
            {
                parameters.IsAnyNullOrWhiteSpaceThrow();
            }

            if (!Directory.Exists(input.FilePath))
            {
                throw new ArgumentException(@"Source path not found. ", nameof(input.FilePath));
            }

            var localRoot   = new DirectoryInfo(input.FilePath);
            var filesToCopy = localRoot.GetFiles(
                input.FileMask ?? "*", // if filemask is not set, get all files.
                options.UploadFromCurrentDirectoryOnly
                    ? SearchOption.TopDirectoryOnly
                    : SearchOption.AllDirectories);

            if (options.ThrowErrorIfNoMatch && filesToCopy.Length < 1)
            {
                throw new ArgumentException(
                          $"No files match the filemask within supplied path. {nameof(input.FileMask)}");
            }

            return(ExecuteUpload(localRoot, filesToCopy, input.S3Directory, parameters, options, cancellationToken));
        }
Ejemplo n.º 3
0
        private static List <string> ExecuteUpload(
            FileSystemInfo localRoot,
            IEnumerable <FileInfo> filesToCopy,
            string s3Directory,
            Parameters parameters,
            UploadOptions options,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            var result = new List <string>();

            using (var client = Utilities.GetS3Client(parameters, cancellationToken))
            {
                var root = Utilities.GetS3Directory(
                    client,
                    s3Directory,
                    parameters.BucketName,
                    cancellationToken);

                foreach (var file in filesToCopy)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if (!file.Exists)
                    {
                        throw new IOException($"Source file does not exist: {file.FullName}");
                    }

                    var s3File = new S3FileInfo(
                        client,
                        parameters.BucketName,
                        Path.Combine(
                            root.Name,
                            options.PreserveFolderStructure
                                ? file.FullName.Replace(Utilities.GetFullPathWithEndingSlashes(localRoot.FullName),
                                                        string.Empty)
                                : file.Name
                            ));

                    s3File = options.DeleteSource
                        ? s3File.MoveFromLocal(file.FullName, options.Overwrite)
                        : s3File.CopyFromLocal(file.FullName, options.Overwrite);

                    result.Add(options.ReturnListOfObjectKeys ? s3File.FullName : file.FullName);
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Prepare to file upload by checking options and file structures.
        /// </summary>
        /// <param name="filesToCopy" />
        /// <param name="s3Directory" />
        /// <param name="parameters" />
        /// <param name="options" />
        /// <param name="cancellationToken" />
        /// <param name="input" />
        /// <returns>List&lt;string&gt;</returns>
        private static async Task <List <string> > ExecuteUpload(
            IEnumerable <FileInfo> filesToCopy,
            string s3Directory,
            Parameters parameters,
            UploadOptions options,
            CancellationToken cancellationToken,
            UploadInput input
            )
        {
            var result = new List <string>();

            using (var client = (AmazonS3Client)Utilities.GetS3Client(parameters))
            {
                foreach (var file in filesToCopy)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if ((file.FullName.Split(Path.DirectorySeparatorChar).Length > input.FilePath.Split(Path.DirectorySeparatorChar).Length&& options.PreserveFolderStructure))
                    {
                        var subfolders = file.FullName.Replace(file.Name, "").Replace(input.FilePath.Replace(file.Name, ""), "").Replace(Path.DirectorySeparatorChar, '/');
                        if (subfolders.StartsWith("/"))
                        {
                            subfolders = subfolders.Remove(0, 1);
                        }
                        var fullPath = s3Directory + subfolders + file.Name;
                        if (!options.Overwrite)
                        {
                            try
                            {
                                var request = new GetObjectRequest
                                {
                                    BucketName = parameters.BucketName,
                                    Key        = fullPath
                                };
                                await client.GetObjectAsync(request, cancellationToken);

                                throw new ArgumentException($"File {file.Name} already exists in S3 at {fullPath}. Set Overwrite-option to true to overwrite the existing file.");
                            }
                            catch (AmazonS3Exception) { }
                        }
                        await UploadFileToS3(cancellationToken, file, parameters, client, fullPath, input);

                        result.Add(options.ReturnListOfObjectKeys ? fullPath : file.FullName);
                    }
                    else
                    {
                        if (!options.Overwrite)
                        {
                            try
                            {
                                var request = new GetObjectRequest
                                {
                                    BucketName = parameters.BucketName,
                                    Key        = s3Directory + file.Name
                                };
                                await client.GetObjectAsync(request, cancellationToken);

                                throw new ArgumentException($"File {file.Name} already exists in S3 at {request.Key}. Set Overwrite-option to true to overwrite the existing file.");
                            }
                            catch (AmazonS3Exception) { }
                        }
                        await UploadFileToS3(cancellationToken, file, parameters, client, s3Directory + file.Name, input);

                        if (options.ReturnListOfObjectKeys)
                        {
                            result.Add(s3Directory + file.Name);
                        }
                        else
                        {
                            result.Add(file.FullName);
                        }
                    }
                    if (options.DeleteSource)
                    {
                        Utilities.DeleteSourceFile(client, cancellationToken, parameters.BucketName, file.FullName, false);
                    }
                }
            }
            return(result);
        }