Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件上载到s3存储桶_C#_.net_Amazon Web Services_Amazon S3 - Fatal编程技术网

C# 将文件上载到s3存储桶

C# 将文件上载到s3存储桶,c#,.net,amazon-web-services,amazon-s3,C#,.net,Amazon Web Services,Amazon S3,我正在尝试将我的文件上载到s3存储桶,但我不希望该文件从我的本地计算机上载,而当有人使用该应用程序并上载文件时,该文件应直接上载到我的s3存储桶。!!有没有办法做到这一点?(代码应该在.net中) 这就是我正在做的..它依次在bucket中创建一个文件夹,并从本地机器获取文件路径,并将其上载到bucket 我需要的是,文件不应该保存在本地机器中,而是直接从应用程序获取到s3存储桶 这是WriteIntoS3方法: string filekey = filePath.Substring(fileP

我正在尝试将我的文件上载到s3存储桶,但我不希望该文件从我的本地计算机上载,而当有人使用该应用程序并上载文件时,该文件应直接上载到我的s3存储桶。!!有没有办法做到这一点?(代码应该在.net中)

这就是我正在做的..它依次在bucket中创建一个文件夹,并从本地机器获取文件路径,并将其上载到bucket

我需要的是,文件不应该保存在本地机器中,而是直接从应用程序获取到s3存储桶

这是WriteIntoS3方法:

string filekey = filePath.Substring(filePath.LastIndexOf('\\') + 1);
                using (MemoryStream filebuffer = new MemoryStream(File.ReadAllBytes(filePath)))
                {
                    PutObjectRequest putRequest = new PutObjectRequest
                    {
                        BucketName = this.awsBucketName,
                        Key = "GUARD1" + "/" + filekey,
                        InputStream = filebuffer,
                        ContentType = "application/pkcs8",
                    };

                    client.PutObject(putRequest);

                    GetPreSignedUrlRequest expiryUrlRequest = new GetPreSignedUrlRequest();
                    expiryUrlRequest.BucketName = this.awsBucketName;
                    expiryUrlRequest.Key = filekey;
                    expiryUrlRequest.Expires = DateTime.Now.AddDays(ExpiryDays);

                    string url = client.GetPreSignedURL(expiryUrlRequest);
                    return url;
                }

如果您不想使用本地文件,那么可以使用TransferUtility类将流直接上载到S3

例如:

using Amazon.S3.Transfer;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        var client = new Amazon.S3.AmazonS3Client();
        using (var ms = new MemoryStream()) // Load the data into memorystream from a data source other than a file
        {
            using (var transferUtility = new TransferUtility(client))
            {
                transferUtility.Upload(ms, "bucket", "key");
            }
        }
    }
}

如果您遇到错误,我们会在这里帮助您,而不是在这里为您生成代码。请先发布您的代码,以便我们可以查看您的位置wrong@PranavPatel先生,我不想有人帮我编码!但是有没有办法做到这一点呢!伟大的只需添加如下区域:var client=newamazons3client(RegionEndpoint.USEast1)
using Amazon.S3.Transfer;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        var client = new Amazon.S3.AmazonS3Client();
        using (var ms = new MemoryStream()) // Load the data into memorystream from a data source other than a file
        {
            using (var transferUtility = new TransferUtility(client))
            {
                transferUtility.Upload(ms, "bucket", "key");
            }
        }
    }
}