9

我想将文件从我的 memoryStream 上传到亚马逊 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.Uplaod(string,string)' 的最佳重载方法匹配有一些无效参数”

4

2 回答 2

9

查看Upload Method(stream、bucketName、key)

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;
    }
}
于 2013-10-04T18:24:15.200 回答
5

哈姆雷特是对的。这是一个示例 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);
        }
    }   
于 2013-10-04T20:49:30.253 回答