using UnityEngine;
using Amazon.S3;
using Amazon.S3.Model;
using System.IO;
using Amazon.CognitoIdentity;
using Amazon;
public class s3upload {
private CognitoAWSCredentials credentials;
private string backetNameString;
private RegionEndpoint regionVal;
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="IdentityPoolId">CognitoのIdentify Pool ID</param>
/// <param name="backetName">バケット名</param>
/// <param name="region">Region未指定の場合はUSEast1</param>
public s3upload(string IdentityPoolId, string backetName, RegionEndpoint region=null)
{
if (region == null) { regionVal = RegionEndpoint.USEast1; } //Region未指定の場合はUSEast1を設定
credentials = new CognitoAWSCredentials(IdentityPoolId, regionVal);
backetNameString = backetName;
}
/// <summary>
/// 指定ファイルをS3バケットにアップロードします
/// </summary>
/// <param name="inputFileFullPath">アップロードするローカルファイルパス</param>
/// <param name="uploadS3path">S3パス。fol/filenameと指定するとfolフォルダ以下にアップロードする</param>
public void uploadFileToS3(string inputFileFullPath, string uploadS3path)
{
AmazonS3Client S3Client = new AmazonS3Client(credentials, regionVal);
//ファイル読み込み
var stream = new FileStream(inputFileFullPath,
FileMode.Open, FileAccess.Read, FileShare.Read);
//リクエスト作成
var request = new PostObjectRequest()
{
Bucket = backetNameString,
Key = uploadS3path,
InputStream = stream,
CannedACL = S3CannedACL.Private
};
//アップロード
S3Client.PostObjectAsync(request, (responseObj) =>
{
if (responseObj.Exception == null){
//Success
Debug.Log(uploadS3path + " :Upload successed");
}
else{Debug.LogError(string.Format("\n receieved error {0}", responseObj.Response.HttpStatusCode.ToString()));}
});
}
}
利用するには下記のようにする
using UnityEngine;
using System.Collections;
using Amazon;
public class test : MonoBehaviour {
// Use this for initialization
void Start () {
UnityInitializer.AttachToGameObject(this.gameObject);
string IdentityPoolId = "us-east-1:6f62b9a3-8bbc-xxxx-xxxx-xxxxxxxxxx";
string backetName = "test-bucket-unity";
string inputFileFullPath = @"C:\temp\zipicon.png";
string uploadS3path = "fol1/zipicon.png";
s3upload s3 = new s3upload(IdentityPoolId, backetName);
s3.uploadFileToS3(inputFileFullPath, uploadS3path);
}
// Update is called once per frame
void Update () {
}
}
メモ
Exception: Main thread has not been set, is the AWSPrefab on the scene?エラーは
UnityInitializer.AttachToGameObject(this.gameObject);
で対処
Local Asset Bundle Serverを実行時に Win32Exception: ApplicationName='”C:/Program Files/Unity5.3.5p2/Editor\Data\MonoBleedingEdge\bin\mono.exe”‘エラー が出る。=>一度Build AssetBundleを実行するとエラーが発生しなくなる
Local Asset Bundle Serverは内部でAssetBundleServer.exeが実行されPort7888のウェブサーバーとして機能する。プロジェクトフォルダ内の\AssetBundlesはhttp://localhost:7888/に対応。他のプロジェクトでビルドしたアセットをテストで利用したい場合はSimulationモードではなく、AssetBundleServerを利用すること。
AssetBundle Manager & Example ScenesのLoadAssets.csスクリプトは内部でLoadFromCacheOrDownload関数をもちいてアセットをダウンロードしています。既に読み込んだことのあるAssetbundle名のアセットは、LoadAssets.csスクリプトもLoadFromCacheOrDownload関数もキャッシュから読み込みます。つまり、普通にビルドするアセットバンドルを更新して配信するだけでは、そのアセットが新しいものなのかどうか判断してくれません。