質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

0回答

238閲覧

IOSUnityアプリでS3の読み込み

KoKo25

総合スコア10

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2018/12/02 10:05

UnityでビルドしたアプリでAWSのS3を読み込みたい

unityでIOSアプリを製作しています。
下記のサンプルコードを使用してS3からオブジェクトを読み込みそれをTextに表示させています。
AWS Mobile SDK for Unity Samples
PCのUnity上では、S3を読み込み下のソースコード内のResultTextに表示できています。
しかしそれをIOSアプリとしてビルドし、iphoneで動かすと読み込めまなく、ResulTextに表示できません。
ちなみにIdentityPoolIdやCognitoIdentityRegionなどはスクリプト内で指定しています。

該当のソースコード

C#

1ソースコード 2 3using UnityEngine; 4using System.Collections; 5using UnityEngine.UI; 6using Amazon.S3; 7using Amazon.S3.Model; 8using Amazon.Runtime; 9using System.IO; 10using System; 11using Amazon.S3.Util; 12using System.Collections.Generic; 13using Amazon.CognitoIdentity; 14using Amazon; 15 16namespace AWSSDK.Examples 17{ 18 public class S3Example : MonoBehaviour 19 { 20 string IdentityPoolId = ""; 21 string CognitoIdentityRegion = ""; 22 private RegionEndpoint _CognitoIdentityRegion 23 { 24 get { return RegionEndpoint.GetBySystemName(CognitoIdentityRegion); } 25 } 26 string S3Region = ""; 27 private RegionEndpoint _S3Region 28 { 29 get { return RegionEndpoint.GetBySystemName(S3Region); } 30 } 31 string S3BucketName = ""; 32 string SampleFileName = ""; 33 public Button GetBucketListButton = null; 34 public Button PostBucketButton = null; 35 public Button GetObjectsListButton = null; 36 public Button DeleteObjectButton = null; 37 public Button GetObjectButton = null; 38 public Text ResultText = null; 39 40 void Start() 41 { 42 UnityInitializer.AttachToGameObject(this.gameObject); 43 44 AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest; 45 46 GetObjectButton.onClick.AddListener(() => { GetObject(); }); 47 } 48 49 #region private members 50 51 private IAmazonS3 _s3Client; 52 private AWSCredentials _credentials; 53 54 private AWSCredentials Credentials 55 { 56 get 57 { 58 if (_credentials == null) 59 _credentials = new CognitoAWSCredentials(IdentityPoolId, _CognitoIdentityRegion); 60 return _credentials; 61 } 62 } 63 64 private IAmazonS3 Client 65 { 66 get 67 { 68 if (_s3Client == null) 69 { 70 _s3Client = new AmazonS3Client(Credentials, _S3Region); 71 } 72 //test comment 73 return _s3Client; 74 } 75 } 76 77 #endregion 78 79 80 /// <summary> 81 /// Get Object from S3 Bucket 82 /// </summary> 83 private void GetObject() 84 { 85 86 ResultText.text = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName); 87 Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) => 88 { 89 90 string data = null; 91 var response = responseObj.Response; 92 if (response.ResponseStream != null) 93 { 94 Debug.Log(data+"aiueo"); 95 using (StreamReader reader = new StreamReader(response.ResponseStream)) 96 { 97 data = reader.ReadToEnd(); 98 Debug.Log(data); 99 } 100 101 ResultText.text += "\n"; 102 ResultText.text += data; 103 104 } 105 }); 106 } 107 108 109 /// <summary> 110 /// Get Objects from S3 Bucket 111 /// </summary> 112 public void GetObjects() 113 { 114 ResultText.text = "Fetching all the Objects from " + S3BucketName; 115 116 var request = new ListObjectsRequest() 117 { 118 BucketName = S3BucketName 119 }; 120 121 Client.ListObjectsAsync(request, (responseObject) => 122 { 123 ResultText.text += "\n"; 124 if (responseObject.Exception == null) 125 { 126 ResultText.text += "Got Response \nPrinting now \n"; 127 responseObject.Response.S3Objects.ForEach((o) => 128 { 129 ResultText.text += string.Format("{0}\n", o.Key); 130 }); 131 } 132 else 133 { 134 ResultText.text += "Got Exception \n"; 135 } 136 }); 137 } 138 139 #region helper methods 140 141 private string GetFileHelper() 142 { 143 var fileName = SampleFileName; 144 145 if (!File.Exists(Application.persistentDataPath + Path.DirectorySeparatorChar + fileName)) 146 { 147 var streamReader = File.CreateText(Application.persistentDataPath + Path.DirectorySeparatorChar + fileName); 148 streamReader.WriteLine("This is a sample s3 file uploaded from unity s3 sample"); 149 streamReader.Close(); 150 } 151 return fileName; 152 } 153 154 private string GetPostPolicy(string bucketName, string key, string contentType) 155 { 156 bucketName = bucketName.Trim(); 157 158 key = key.Trim(); 159 // uploadFileName cannot start with / 160 if (!string.IsNullOrEmpty(key) && key[0] == '/') 161 { 162 throw new ArgumentException("uploadFileName cannot start with / "); 163 } 164 165 contentType = contentType.Trim(); 166 167 if (string.IsNullOrEmpty(bucketName)) 168 { 169 throw new ArgumentException("bucketName cannot be null or empty. It's required to build post policy"); 170 } 171 if (string.IsNullOrEmpty(key)) 172 { 173 throw new ArgumentException("uploadFileName cannot be null or empty. It's required to build post policy"); 174 } 175 if (string.IsNullOrEmpty(contentType)) 176 { 177 throw new ArgumentException("contentType cannot be null or empty. It's required to build post policy"); 178 } 179 180 string policyString = null; 181 int position = key.LastIndexOf('/'); 182 if (position == -1) 183 { 184 policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" + 185 bucketName + "\"},[\"starts-with\", \"$key\", \"" + "\"],{\"acl\": \"private\"},[\"eq\", \"$Content-Type\", " + "\"" + contentType + "\"" + "]]}"; 186 } 187 else 188 { 189 policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" + 190 bucketName + "\"},[\"starts-with\", \"$key\", \"" + key.Substring(0, position) + "/\"],{\"acl\": \"private\"},[\"eq\", \"$Content-Type\", " + "\"" + contentType + "\"" + "]]}"; 191 } 192 193 return policyString; 194 } 195 196 } 197 198 #endregion 199} 200

補足情報

unityのバージョンは2018.2.5f1です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問