.NET SDK for Sirv S3
https://s3.sirv.com No account yet? Create one freeShort answer
Use the AWS SDK for .NET with the endpoint s3.sirv.com and the S3 bucket, key and secret from your Sirv account settings. Sirv supports current and older SDK and signature versions. The examples list folders, fetch file information, and download, upload, copy, move and delete files.
After reading this you will know
- How to connect the AWS SDK for .NET to Sirv
- Which SDK and signature versions Sirv supports
- How to upload, download and manage files with .NET
Easily upload and manage files on your Sirv account with the AWS SDK for .NET.
Sirv supports current and historic versions of the AWS .NET SDK and Signature:
| Signature Version 2 | Signature Version 3 | Signature Version 4 | |
|---|---|---|---|
| AWS SDK for .NET v2 | Full capability | Full capability | Partial capability (no upload)* |
| AWS SDK for .NET v3 | Full capability | Partial capability (no upload)* | Partial capability (no upload)* |
*In a later update, Sirv will also support uploads using Signature Version 4. Alternatively, you may prefer to upload files using Sirv’s REST API.
Connect to Sirv
Section titled “Connect to Sirv”1. Download and unzip the Sirv Console App for Visual Studio (zip).
2. Open the file SirvConsoleApp.csproj to install the console app. (Assuming you use Visual Studio).
3. Install either the S3 SDK v3 or the S3 SDK v2 from NuGet.
4. Connect to Sirv using the example script below:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace SirvConsoleApp{ public class Program { public static void Main(string[] args) { // Paste your S3 access keys and bucket name below, found on your Sirv settings page: https://app.sirv.com/settings/uploads var awsAccessKeyId = "ENTER_YOUR_SIRV_S3_KEY_HERE,"; var awsSecretAccessKey = "ENTER_YOUR_SIRV_S3_SECRET_HERE,"; var serviceUrl = "https://s3.sirv.com"; var bucketName = "ENTER_YOUR_SIRV_S3_BUCKET_HERE";
// Prepares your AWS credentials for connection Amazon.Runtime.AWSCredentials credentials = new Amazon.Runtime.BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey);
// Prepares the Amazon S3 configuration for connection Amazon.S3.AmazonS3Config config = new Amazon.S3.AmazonS3Config { SignatureVersion = "2", // Note: versions 2, 3 and 4 are supported by s3.sirv.com AuthenticationRegion = "", // Important: should be empty to connect to s3.sirv.com ServiceURL = serviceUrl, ForcePathStyle = true };
using (Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(credentials, config)) { // Enter your commands here - see below for example commands } } }}Now you can use the .NET commands below to perform common file actions such as check/copy/move/delete/upload files.
You can also find these commands in the Program.cs file of the Sirv Console App for Visual Studio (zip).
List folder contents
Section titled “List folder contents”// Fetch a list of all files in a folderAmazon.S3.Model.ListObjectsRequest listObjectRequest = new Amazon.S3.Model.ListObjectsRequest{ BucketName = bucketName, Prefix = "examplefolder/examplesubfolder", // Enter the folder location or use "" to return files in the root};
List<amazon.s3.model.s3object> objects = new List<amazon.s3.model.s3object>();Amazon.S3.Model.ListObjectsResponse response;do{ response = client.ListObjects(listObjectRequest); objects.AddRange(response.S3Objects); listObjectRequest.Marker = response.NextMarker;}while (response.IsTruncated);Amazon.S3.Model.S3Object firstObject = objects[1];// If you set Prefix to "" then change the above to objects[0]. If you set Prefix as a folder, the first item returned will be the folder name, the second item returned will be the first file. If you set Prefix to a non-existing folder or path, an exception will be thrown: Amazon.S3.AmazonS3Exception: 'No such file or directory: /nosuchfolder/example/etc'.</amazon.s3.model.s3object></amazon.s3.model.s3object>Note: the .NET SDK sometimes provides an incomplete list, therefore if the list is truncated, the script fetches the next set of records.
Fetch file information
Section titled “Fetch file information”// Fetch the information for a particular file (this also permits downloading of the file)Amazon.S3.Model.GetObjectResponse knownObjectDetails = client.GetObject(bucketName, "examplefolder/image.jpg");
// Check the file name and size, in byteslong size = knownObjectDetails.ContentLength;
// Check the file last modified timevar lastModified = knownObjectDetails.LastModified;Download a file
Section titled “Download a file”// Download a particular filefirstObjectDetails.WriteResponseStreamToFile(@"D:\\examplefolder/image.jpg");Console.WriteLine($@"{objectKey} downloaded to D:\\examplefolder/image.jpg");Upload a file
Section titled “Upload a file”// Upload a local fileclient.PutObject(new Amazon.S3.Model.PutObjectRequest() { FilePath = @"D:\\examplefolder/image.jpg", BucketName = bucketName, Key = "image.jpg" });Console.WriteLine($@"D:\\examplefolder/image.jpg uploaded to image.jpg");
// Upload a local file to a particular folder (if the folder does not exist, it will be created)client.PutObject(new Amazon.S3.Model.PutObjectRequest() { FilePath = @"D:\\examplelocalfolder/image.jpg", BucketName = bucketName, Key = "examplefolder/image.jpg" });Console.WriteLine($@"D:\\examplelocalfolder/image.jpg uploaded to examplefolder/image.jpg");Copy a file
Section titled “Copy a file”// Copy a file, by specifying the existing file name and the copied file nameclient.CopyObject(bucketName, "examplefolder/examplesubfolder/image.jpg", bucketName, "examplefolder/examplesubfolder/image copy.jpg");Console.WriteLine($@"examplefolder/examplesubfolder/image.jpg copied to examplefolder/examplesubfolder/image copy.jpg");Move a file
Section titled “Move a file”// Move a file (there is no S3 API command to move a file, so the original must be copied then deleted)client.CopyObject(bucketName, "examplefolder/image.jpg", bucketName, "examplefolder/examplesubfolder/image.jpg");client.DeleteObject(bucketName, "examplefolder/image.jpg");Console.WriteLine($@"examplefolder/image.jpg moved to examplefolder/examplesubfolder/image.jpg");Delete a file
Section titled “Delete a file”// Delete a file (file will be sent to Trash in your Sirv account for 30 days)client.DeleteObject(bucketName, "examplefolder/examplesubfolder/image.jpg");Console.WriteLine($@"examplefolder/examplesubfolder/image.jpg sent to trash");Deleted files stay in Trash in your Sirv account for 30 days, so you can restore them if you need to.
