Skip to main content

AWS .Net SDK Example

CAUTION

Chunk encoding is not supported by the Cloud Storage API. Please set putObjectRequest.UseChunkEncoding = false

The following example shows how AWS .Net SDK can be used to interact with Telnyx Cloud Storage.

using System.Text;
using System.Text.Json;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;

// 1. Configure and create Telnyx API Client

var apiKeyTelnyx = "API-KEY-HERE";
var bucketName = "BUCKET-NAME-HERE";
var objectName = "OBJECT-NAME-HERE";
var region = "us-central-1";

var s3Config = new AmazonS3Config
{
ServiceURL = $"https://{region}.telnyxcloudstorage.com",
ForcePathStyle = true,
LogResponse = true,
DisableLogging = false,
SignatureVersion = "4",
};

var telnyxClient = new AmazonS3Client(
new BasicAWSCredentials(apiKeyTelnyx, apiKeyTelnyx),
s3Config
);

// 2. Create Bucket

var createBucketRequest = new PutBucketRequest
{
BucketName = bucketName
};
var createBucketResponse = await telnyxClient.PutBucketAsync(createBucketRequest);

// 3. Upload object

var putObjectRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = "objectName",
FilePath = "/Users/yiuming/test.txt"
};
// IMPORTANT: chunk encoding is not supported by the Cloud Storage API.
putObjectRequest.UseChunkEncoding = false;
var putObjectResponse = await telnyxClient.PutObjectAsync(putObjectRequest);

// 4. List Objects

var listObjectRequest = new ListObjectsRequest
{
BucketName = bucketName
};
var listObjectResponse = await telnyxClient.ListObjectsAsync(listObjectRequest);

// 5. List Buckets

var listBucketsRequest = new ListBucketsRequest { };
var listBucketsResponse = await telnyxClient.ListBucketsAsync(listBucketsRequest);

// 6. Download Object

var getObjectRequest = new GetObjectRequest
{
BucketName = bucketName,
Key = objectName
};
var getObjectResponse = await telnyxClient.GetObjectAsync(getObjectRequest);

// 7. Presigned URL Get
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.telnyx.com");
var presignedUrlRequest = new Dictionary<string, object>
{
{ "ttl", 200 }
};
var presignedUrlRequestJson = JsonSerializer.Serialize(presignedUrlRequest);
var presignedUrlRequestContent = new StringContent(
presignedUrlRequestJson,
Encoding.UTF8,
"application/json");

client.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiKeyTelnyx);
var presignedUrlResponse =
await client.PostAsync($"/v2/storage/buckets/{bucketName}/{objectName}/presigned_url",
presignedUrlRequestContent);
var content = await presignedUrlResponse.Content.ReadAsStringAsync();
Console.WriteLine(content);
}