如何将内存文件上传到Amazon S3?

9

我想将内存流中的文件上传到Amazon S3服务器。

以下是代码:

public static bool memUploadFile(AmazonS3 client, MemoryStream memFile, string toPath)
{

    try
    {
        Amazon.S3.Transfer.TransferUtility tranUtility = new Amazon.S3.Transfer.TransferUtility(client);
        tranUtility.Upload(filePath, toPath.Replace("\\", "/"));

        return true;
    }
    catch (Exception ex)
    {
        return false;
    }

}

然后错误消息指出:
“Amazon.S3.Transfer.TransferUtility.Upload(string,string)的最佳重载方法匹配存在一些无效的参数”。

您没有指定filePath指向哪里。此外,“a file from my memorystream”没有意义。 - piksel bitworks
2个回答

9
看看上传方法(流,桶名称,键)
public static bool memUploadFile(AmazonS3 client, MemoryStream memFile, string toPath)
{
    try
    {
        using(Amazon.S3.Transfer.TransferUtility tranUtility =
                      new Amazon.S3.Transfer.TransferUtility(client))
        {
            tranUtility.Upload(memFile, toPath.Replace("\\", "/"), <The key under which the Amazon S3 object is stored.>);

            return true;
        }
    }
    catch (Exception ex)
    {
        return false;
    }
}

6

哈姆雷特是正确的。这是一个TransferUtilityUploadRequest的示例。

    [Test]
    public void UploadMemoryFile()
    {
        var config = CloudConfigStorage.GetAdminConfig();

        string bucketName = config.BucketName;
        string clientAccessKey = config.ClientAccessKey;
        string clientSecretKey = config.ClientSecretKey;

        string path = Path.GetFullPath(@"dummy.txt");
        File.WriteAllText(path, DateTime.Now.ToLongTimeString());

        using (var client = AWSClientFactory.CreateAmazonS3Client(clientAccessKey, clientSecretKey))
        using (var transferUtility = new TransferUtility(client))
        {

            var request = new TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                Key = "memory.txt",
                InputStream = new MemoryStream(File.ReadAllBytes(path))
            };

            transferUtility.Upload(request);
        }
    }   

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接