using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;
using UnityEngine;

public class UploadImageIAMUser : MonoBehaviour
{
    public IAmazonS3 s3Client;

    void Start()
    {
        AmazonS3Config s3Config = new AmazonS3Config();
        s3Config.RegionEndpoint = Amazon.RegionEndpoint.USEast1;

        // Building S3 client with Access Key and Secret Access Key
        s3Client = new AmazonS3Client("AKIAUUCCXU7QHL2TK37P", "lXu3H7vuoi9krlVQ8LpaPDJCmEXvnhOh56t1U3tT", s3Config);
    }

    public async void Pick()
    {
        // Picking image from device
        NativeGallery.GetImageFromGallery(async (path) =>
        {
            if (path != null)
            {
                // Uploading image to an S3 bucket
                bool uploaded = await UploadFileAsync(s3Client, "image_uploaded_with_iam_user.jpg", path);
                print($"Uploaded: {uploaded}");
            }
        });
    }

    private static async Task<bool> UploadFileAsync(IAmazonS3 client, string objectName, string filePath)
    {
        var request = new PutObjectRequest
        {
            BucketName = "awsresourcesbucket",
            Key = objectName,
            FilePath = filePath,
        };

        var response = await client.PutObjectAsync(request);

        return (response.HttpStatusCode == System.Net.HttpStatusCode.OK);
    }
}