Advertisement
NovusX

awsManager

Jul 30th, 2021
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using Amazon.S3;
  5. using Amazon.S3.Model;
  6. using Amazon.Runtime;
  7. using System.IO;
  8. using System;
  9. using Amazon.S3.Util;
  10. using System.Collections.Generic;
  11. using Amazon.CognitoIdentity;
  12. using Amazon;
  13. using System.Linq;
  14. using System.Runtime.Serialization.Formatters.Binary;
  15.  
  16. public class AWSManager : MonoBehaviour
  17. {
  18.     private static AWSManager _instance;
  19.     public static AWSManager Instance
  20.     {
  21.         get
  22.         {
  23.             if(_instance == null )
  24.             {
  25.                 Debug.LogError("AWS Manager is Null");
  26.             }
  27.             return _instance;
  28.         }
  29.     }
  30.  
  31.     private AmazonS3Client _S3Client;
  32.     public AmazonS3Client S3Client
  33.     {
  34.         get
  35.         {
  36.             if(_S3Client == null)
  37.             {
  38.                 _S3Client = new AmazonS3Client(new CognitoAWSCredentials(
  39.                  "us-east-2:32690323-db25-474c-87de-92cee4c0071f",
  40.                  RegionEndpoint.USEast2
  41.                  ), _S3Region);
  42.             }
  43.  
  44.             return _S3Client;
  45.         }
  46.     }
  47.  
  48.     public string S3Region = RegionEndpoint.USEast2.SystemName;
  49.     private RegionEndpoint _S3Region
  50.     {
  51.         get { return RegionEndpoint.GetBySystemName(S3Region); }
  52.     }
  53.  
  54.     private void Awake()
  55.     {
  56.         _instance = this;
  57.  
  58.         UnityInitializer.AttachToGameObject(this.gameObject);
  59.         AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;
  60.  
  61.     }
  62.  
  63.     public void UploadToS3(string path, string caseID)
  64.     {
  65.         FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  66.  
  67.         PostObjectRequest objectRequest = new PostObjectRequest()
  68.         {
  69.             Bucket = "gdvq.enterprise.cases",
  70.             Key = "case#" + caseID,
  71.             InputStream = stream,
  72.             CannedACL = S3CannedACL.Private,
  73.             Region = _S3Region
  74.         };
  75.  
  76.         S3Client.PostObjectAsync(objectRequest, (responseObject) =>
  77.         {
  78.             if (responseObject.Exception == null)
  79.             {
  80.                 UIManager.Instance.StartCoroutine(UIManager.Instance.SuccessRoutine());
  81.             }
  82.             else
  83.             {
  84.                 Debug.LogWarning("Error occured during case upload: " + responseObject.Exception);
  85.             }
  86.         });
  87.  
  88.     }
  89.  
  90.     public void GetS3List(string caseNumber, Action onComplete = null)
  91.     {
  92.         string target = "case#" + caseNumber;
  93.        
  94.         var request = new ListObjectsRequest()
  95.         {
  96.             BucketName = "gdvq.enterprise.cases"
  97.         };
  98.  
  99.         S3Client.ListObjectsAsync(request, (responseObject) =>
  100.         {
  101.             if (responseObject.Exception == null)
  102.             {
  103.                 bool caseFound = responseObject.Response.S3Objects.Any(obj => obj.Key ==  target);
  104.                 if(caseFound)
  105.                 {
  106.                     //download case
  107.                     DownloadCase(onComplete, target);
  108.                 }
  109.                 else
  110.                 {
  111.                     UIManager.Instance.StartCoroutine(UIManager.Instance.CaseNotFoundRoutine());
  112.                 }
  113.             }
  114.             else
  115.             {
  116.                 Debug.Log("Listing Object errored from S3: " + responseObject.Exception);
  117.             }
  118.         });
  119.     }
  120.  
  121.     private void DownloadCase(Action onComplete, string target)
  122.     {
  123.         S3Client.GetObjectAsync("gdvq.enterprise.cases", target, (responseObject) =>
  124.         {
  125.             if (responseObject.Exception == null)
  126.             {
  127.                 //read data and apply it to case
  128.  
  129.                 //check respose stream
  130.                 if (responseObject.Response.ResponseStream != null)
  131.                 {
  132.                     //create a byte array
  133.                     byte[] data = null;
  134.  
  135.                     //use steamreader to read data
  136.                     using (StreamReader reader = new StreamReader(responseObject.Response.ResponseStream))
  137.                     {
  138.                         //access memory stream
  139.                         using (MemoryStream memory = new MemoryStream())
  140.                         {
  141.                             var buffer = new byte[512];
  142.                             var bytesRead = default(int);
  143.  
  144.                             while ((bytesRead = reader.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
  145.                             {
  146.                                 memory.Write(buffer, 0, bytesRead);
  147.                             }
  148.  
  149.                             //populate data
  150.                             data = memory.ToArray();
  151.                         }
  152.                     }
  153.  
  154.                     //convert byte data to a case object
  155.                     using (MemoryStream memory = new MemoryStream(data))
  156.                     {
  157.                         BinaryFormatter bf = new BinaryFormatter();
  158.                         Case downloadedCase = (Case)bf.Deserialize(memory);
  159.                         //Assign the active case to the downloaded case
  160.                         UIManager.Instance.activeCase = downloadedCase;
  161.  
  162.                         //send a message to unity that download is complete
  163.                         if (onComplete != null) onComplete();
  164.                     }
  165.                 }
  166.             }
  167.         });
  168.     }
  169.  
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement