I have worked on a Xamarin.Forms project to make an app both for android and ios devices. I have maintained files using amazon s3 file storage service and I have done this job making dependency service which is really a life maker for a cross platform like Xamarin.Forms to bring native flavor. However, I will show step by step how I have integrated amazon s3 service.
First of all, I need to make an interface like below:
using System.IO;
using System.Threading.Tasks;
namespace Services
{
public interface IAmazonS3Service
{
Task<string> UploadFile(string filepath, string filename);
string GetAmazonS3Url(string keyName);
void UploadStream(MemoryStream stream, string keyName);
}
}
then I will keep this IAmazonS3Service interface into our portable project like Services (Portable).
Now I will create a class named AmazonS3Service will inherit IAmazonS3Service and put this into our iOS project.
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.IO;
using System.Threading.Tasks;
using iOS;
using Services;
[assembly: Xamarin.Forms.Dependency(typeof(AmazonS3Service))]
namespace iOS
{
public class AmazonS3Service: IAmazonS3Service
{
const string bucketName = "bucketName";
const string accessKey = "accessKey";
const string secretKey = "secretKey";
public async Task<string> UploadFile(string filepath, string filename)
{
try
{
AmazonS3Client client = SetAmazonCredential();
PutObjectRequest request = new PutObjectRequest
{
BucketName = bucketName,
Key = filename,
FilePath = filepath
};
// Put object
PutObjectResponse response = await client.PutObjectAsync(request);
return "File uploaded to S3 Bucket";
}
catch (AmazonS3Exception s3Exception)
{
return "Upload failed. " + s3Exception.Message;
}
}
public string GetAmazonS3Url(string keyName)
{
var url = string.Empty;
try
{
AmazonS3Client client = SetAmazonCredential();
var expiryUrlRequest = new GetPreSignedUrlRequest()
{
BucketName = bucketName,
Key = keyName,
Expires = DateTime.Now.AddDays(7)
};
url = client.GetPreSignedURL(expiryUrlRequest);
}
catch (AmazonS3Exception s3Exception)
{
//
}
return url;
}
public async void UploadStream(MemoryStream stream, string keyName)
{
try
{
AmazonS3Client client = SetAmazonCredential();
PutObjectRequest request = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
InputStream = stream
};
// Put object
PutObjectResponse response = await client.PutObjectAsync(request);
}
catch (AmazonS3Exception s3Exception)
{
}
}
private static AmazonS3Client SetAmazonCredential()
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var client = new AmazonS3Client(credentials, RegionEndpoint.APSouth1);
return client;
}
}
}
I will create another class with the same name AmazonS3Service inheriting IAmazonS3Service and will put this one into our Droid project.
using System;
using Services;
using System.Threading.Tasks;
using Amazon.S3.Model;
using Amazon.S3;
using Amazon.Runtime;
using Amazon;
using Droid;
using System.IO;
[assembly: Xamarin.Forms.Dependency(typeof(AmazonS3Service))]
namespace Droid
{
public class AmazonS3Service: IAmazonS3Service
{
const string bucketName = "bucketName";
const string accessKey = "accessKey";
const string secretKey = "secretKey";
public async Task<string> UploadFile(string filepath, string filename)
{
try
{
AmazonS3Client client = SetAmazonCredential();
PutObjectRequest request = new PutObjectRequest
{
BucketName = bucketName,
Key = filename,
FilePath = filepath
};
// Put object
PutObjectResponse response = await client.PutObjectAsync(request);
return "File uploaded to S3 Bucket";
}
catch (AmazonS3Exception s3Exception)
{
return "Upload failed. " + s3Exception.Message;
}
}
public string GetAmazonS3Url(string keyName)
{
var url = string.Empty;
try
{
AmazonS3Client client = SetAmazonCredential();
var expiryUrlRequest = new GetPreSignedUrlRequest() {
BucketName = bucketName,
Key = keyName,
Expires = DateTime.Now.AddDays(7)
};
url = client.GetPreSignedURL(expiryUrlRequest);
}
catch (AmazonS3Exception s3Exception)
{
//
}
return url;
}
public async void UploadStream(MemoryStream stream, string keyName)
{
try
{
AmazonS3Client client = SetAmazonCredential();
PutObjectRequest request = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
InputStream = stream
};
// Put object
PutObjectResponse response = await client.PutObjectAsync(request);
}
catch (AmazonS3Exception s3Exception)
{
}
}
private static AmazonS3Client SetAmazonCredential()
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var client = new AmazonS3Client(credentials, RegionEndpoint.APSouth1);
return client;
}
}
}
Now I will call those dependency services using this code below:
DependencyService.Get<IAmazonS3Service>().DownloadStream(keyName);
you can also call remaining methods like above code snippet.