在amazon S3中创建zip文件夹

本文关键字:zip 文件夹 创建 amazon S3 | 更新日期: 2023-09-27 18:03:30

我必须在amazon S3中创建一个文件夹。现在必须在zip文件中转换该文件夹。我使用DotNetZip库将其转换为.zip文件。这是那个

的链接http://dotnetzip.codeplex.com/wikipage?title=CS-Examples

public void ConvertToZip(string directoryToZip, string zipFileName)
{
    try
    {
        using (client = DisposableAmazonClient())
        {
            var sourDir = new S3DirectoryInfo(client, bucket, directoryToZip);
            var destDir = new S3DirectoryInfo(client, bucket, CCUrlHelper.BackupRootFolderPhysicalPath);
            using (var zip = new ZipFile())
            {
                zip.AddDirectory(sourDir.FullName); // recurses subdirectories
                zip.Save(Path.Combine(destDir.FullName, zipFileName));
            }
        }
        logger.Fatal("Successfully converted to Zip.");
    }
    catch (Exception ex)
    {
        logger.Error("Error while converting to zip. Error : " + ex.Message);
    }
}

当我运行代码时,它显示错误"给定路径的格式不受支持"。

在amazon S3中创建zip文件夹

S3DirectoryInfo模拟目录结构,但它不是实际的目录结构,DotNetZip不知道如何处理指向S3中的对象的字符串。为了做到这一点,您必须下载这些文件,对它们进行压缩,然后上传生成的压缩文件。下面是一些示例代码,展示了如何做到这一点。

<>之前类项目{静态void Main(string[] args){var zipFilename = @"c:'temp'data.zip";var client = new AmazonS3Client();S3DirectoryInfo rootDir = new S3DirectoryInfo(client, "norm-ziptest");使用(var zip = new ZipFile()){邮政编码。Name = zipFilename;addFiles(zip, rootDir, ");}

    // Move local zip file to S3
    var fileInfo = rootDir.GetFile("data.zip");
    fileInfo.MoveFromLocal(zipFilename);
}
static void addFiles(ZipFile zip, S3DirectoryInfo dirInfo, string archiveDirectory)
{
    foreach (var childDirs in dirInfo.GetDirectories())
    {
        var entry = zip.AddDirectoryByName(childDirs.Name);
        addFiles(zip, childDirs, archiveDirectory + entry.FileName);
    }
    foreach (var file in dirInfo.GetFiles())
    {
        using (var stream = file.OpenRead())
        {                    
            zip.AddEntry(archiveDirectory + file.Name, stream);
            // Save after adding the file because to force the 
            // immediate read from the S3 Stream since 
            // we don't want to keep that stream open.
            zip.Save(); 
        }
    }
}

}