multiple xr toolkit package
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

S3Manager.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright 2014-2015 Amazon.com,
  3. // Inc. or its affiliates. All Rights Reserved.
  4. //
  5. // Licensed under the AWS Mobile SDK For Unity
  6. // Sample Application License Agreement (the "License").
  7. // You may not use this file except in compliance with the
  8. // License. A copy of the License is located
  9. // in the "license" file accompanying this file. This file is
  10. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  11. // CONDITIONS OF ANY KIND, express or implied. See the License
  12. // for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. using UnityEngine;
  16. using System.Collections;
  17. using UnityEngine.UI;
  18. using Amazon.S3;
  19. using Amazon.S3.Model;
  20. using Amazon.Runtime;
  21. using System.IO;
  22. using System;
  23. using Amazon.S3.Util;
  24. using System.Collections.Generic;
  25. using Amazon.CognitoIdentity;
  26. using Amazon;
  27. public class S3Manager : MonoBehaviour
  28. {
  29. public string IdentityPoolId = "us-east-1:066cd25b-b249-4394-a267-7a49247aa8f9"; // vrchat identity pool id
  30. public string CognitoIdentityRegion = RegionEndpoint.USEast1.SystemName;
  31. private RegionEndpoint _CognitoIdentityRegion
  32. {
  33. get { return RegionEndpoint.GetBySystemName(CognitoIdentityRegion); }
  34. }
  35. public string S3Region = RegionEndpoint.USEast1.SystemName;
  36. private RegionEndpoint _S3Region
  37. {
  38. get { return RegionEndpoint.GetBySystemName(S3Region); }
  39. }
  40. public string S3BucketName = "vrc-uploads"; // vrchat
  41. private IAmazonS3 _s3Client;
  42. private AWSCredentials _credentials;
  43. private AWSCredentials Credentials
  44. {
  45. get
  46. {
  47. if (_credentials == null)
  48. _credentials = new CognitoAWSCredentials(IdentityPoolId, _CognitoIdentityRegion);
  49. return _credentials;
  50. }
  51. }
  52. private IAmazonS3 Client
  53. {
  54. get
  55. {
  56. if (_s3Client == null)
  57. {
  58. _s3Client = new AmazonS3Client(Credentials, _S3Region);
  59. }
  60. //test comment
  61. return _s3Client;
  62. }
  63. }
  64. void Start()
  65. {
  66. UnityInitializer.AttachToGameObject(this.gameObject);
  67. }
  68. /// <summary>
  69. /// Post Object to S3 Bucket.
  70. /// </summary>
  71. public PostObjectRequest PostObject(string filePath, string s3FolderName, Action<string> onSuccess = null)
  72. {
  73. string fileName = s3FolderName + "/" + System.IO.Path.GetFileName(filePath);
  74. VRC.Core.Logger.Log ("uploading " + fileName, VRC.Core.DebugLevel.All);
  75. AWSConfigs.LoggingConfig.LogTo = LoggingOptions.None;
  76. var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  77. VRC.Core.Logger.Log ("Creating request object", VRC.Core.DebugLevel.All);
  78. var request = new PostObjectRequest()
  79. {
  80. Bucket = S3BucketName,
  81. Key = fileName,
  82. InputStream = stream,
  83. CannedACL = S3CannedACL.Private
  84. };
  85. VRC.Core.Logger.Log ("Making HTTP post call", VRC.Core.DebugLevel.All);
  86. StartCoroutine(PostObjectRoutine(request, onSuccess));
  87. return request;
  88. }
  89. IEnumerator PostObjectRoutine(PostObjectRequest request, Action<string> onSuccess)
  90. {
  91. // make sure UnityInitializer call has time to initialize
  92. yield return null;
  93. yield return null;
  94. Client.PostObjectAsync(request, (responseObj) =>
  95. {
  96. if (responseObj.Exception == null)
  97. {
  98. VRC.Core.Logger.Log("object " + responseObj.Request.Key + " posted to bucket " + responseObj.Request.Bucket, VRC.Core.DebugLevel.All);
  99. string s3Url = string.Format("https://s3-us-west-2.amazonaws.com/{0}/{1}", responseObj.Request.Bucket, responseObj.Request.Key);
  100. if(onSuccess != null)
  101. onSuccess(s3Url);
  102. }
  103. else
  104. {
  105. VRC.Core.Logger.Log ("Exception while posting the result object");
  106. VRC.Core.Logger.Log("receieved error " + responseObj.Response.HttpStatusCode.ToString());
  107. }
  108. });
  109. }
  110. }