# .NET SDK for Sirv S3

Easily upload and manage files on your Sirv account with the [AWS SDK for .NET](https://aws.amazon.com/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](/help/rest-api/reference/upload-download/upload-file/)._

### Connect to Sirv

1\. Download and unzip the [Sirv Console App for Visual Studio](https://sirv.com/download/SirvConsoleAppNet.zip) (zip).

2\. Open the file **SirvConsoleApp.csproj** to install the console app. (Assuming you use [Visual Studio](https://visualstudio.microsoft.com/downloads/)).

3\. Install either the [S3 SDK v3](https://www.nuget.org/packages/AWSSDK.S3/) or the [S3 SDK v2](https://www.nuget.org/packages/AWSSDK/) from NuGet.

4\. Connect to Sirv using the example script below:

```php
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](https://sirv.com/download/SirvConsoleAppNet.zip) (zip).

### List folder contents

```php
// Fetch a list of all files in a folder
Amazon.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

```php
// 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 bytes
long size = knownObjectDetails.ContentLength;

// Check the file last modified time
var lastModified = knownObjectDetails.LastModified;
```

### Download a file

```php
// Download a particular file
firstObjectDetails.WriteResponseStreamToFile(@"D:\\examplefolder/image.jpg");
Console.WriteLine($@"{objectKey} downloaded to D:\\examplefolder/image.jpg");
```

### Upload a file

```php
// Upload a local file
client.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

```php
// Copy a file, by specifying the existing file name and the copied file name
client.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

```php
// 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

```php
// 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.
