How to upload a file to amazon S3 super easy using c#

前端 未结 5 934
长发绾君心
长发绾君心 2020-12-07 12:44

I am tired of all these \"upload to S3\" examples and tutorials that don\'t work , can someone just show me an example that simply works and is super easy?

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 13:27

    I have written a tutorial about this.

    Uploading a file to S3 bucket using low-level API:

    IAmazonS3 client = new AmazonS3Client("AKI...access-key...", "+8Bo...secrey-key...", RegionEndpoint.APSoutheast2);  
    
    FileInfo file = new FileInfo(@"c:\test.txt");  
    string destPath = "folder/sub-folder/test.txt"; // <-- low-level s3 path uses /
    PutObjectRequest request = new PutObjectRequest()  
    {  
        InputStream = file.OpenRead(),  
        BucketName = "my-bucket-name",  
        Key = destPath // <-- in S3 key represents a path  
    };  
      
    PutObjectResponse response = client.PutObject(request); 
    

    Uploading a file to S3 bucket using high-level API:

    IAmazonS3 client = new AmazonS3Client("AKI...access-key...", "+8Bo...secrey-key...", RegionEndpoint.APSoutheast2);  
    
    FileInfo localFile = new FileInfo(@"c:\test.txt");  
    string destPath = @"folder\sub-folder\test.txt"; // <-- high-level s3 path uses \
     
    S3FileInfo s3File = new S3FileInfo(client, "my-bucket-name", destPath);  
    if (!s3File.Exists)  
    {  
        using (var s3Stream = s3File.Create()) // <-- create file in S3  
        {  
            localFile.OpenRead().CopyTo(s3Stream); // <-- copy the content to S3  
        }  
    }  
    

提交回复
热议问题