Skip to content

Instantly share code, notes, and snippets.

@GRGSIBERIA
Created January 15, 2014 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GRGSIBERIA/8441237 to your computer and use it in GitHub Desktop.
Save GRGSIBERIA/8441237 to your computer and use it in GitHub Desktop.
Unityで撮ったスクリーンショットをAmazon S3にアップロードする
using UnityEngine;
using System.Collections;
using System.Text;
using System;
using System.IO;
using System.Collections.Generic;
using System.Security.Cryptography;
using Amazon.S3.Transfer;
using Amazon.S3;
using Amazon;
using System.Linq;
public class ScreenShotScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
byte[] MakeScreenShot()
{
var texture = camera.targetTexture;
var tex2d = new Texture2D(texture.width, texture.height);
RenderTexture.active = texture;
tex2d.ReadPixels(new Rect(0, 0, tex2d.width, tex2d.height), 0, 0);
tex2d.Apply();
return tex2d.EncodeToPNG();
}
string EncodeSHA2(string name)
{
var crypt = new MD5CryptoServiceProvider();
var name_bytes = Encoding.UTF8.GetBytes(name);
var crypted_bytes = crypt.ComputeHash(name_bytes); // ここで時間をハッシュに変える
crypt.Clear();
return BitConverter.ToString(crypted_bytes).ToLower().Replace("-", "");
}
// 鍵のペアはテキストアセットで用意しておく
Dictionary<string, string> ReadAccessSecretFromResource()
{
var text = Resources.Load("access_secret") as TextAsset;
var kv = new Dictionary<string, string>();
var splited = text.text.Split('\n');
kv["AccessKey"] = splited[0];
kv["SecretKey"] = splited[1];
return kv;
}
// ファイル名を生成する
string MakeKeyName()
{
string key_name = System.DateTime.Now.ToString() + Time.realtimeSinceStartup; // 現在の時間と起動時間からファイル名を生成する
return EncodeSHA2(key_name) + ".png"; // 最後にSHA2に変換する
}
// アップロードするよー
void UploadFile(byte[] png)
{
// クライアントの作成
var kv = ReadAccessSecretFromResource();
var s3 = AWSClientFactory.CreateAmazonS3Client(
kv["AccessKey"],
kv["SecretKey"],
new AmazonS3Config { ServiceURL = "http://fukuwarai.s3-website-ap-northeast-1.amazonaws.com" });
// Amazonに接続するためのクラスを作成する
using (var transfer = new TransferUtility(s3))
{
var key_name = MakeKeyName(); // ファイル名のこと
// 必要な情報を入れている
var request = new TransferUtilityUploadRequest();
request.BucketName = "fukuwarai";
request.Key = key_name;
request.InputStream = new MemoryStream(png); // バイナリを送信するときはこうする
request.PartSize = 1024 * 1024 * 1; // 1MB区切りで送信
request.StorageClass = S3StorageClass.Standard;
request.ContentType = "image/png";
request.CannedACL = S3CannedACL.PublicRead;
Log(key_name, png, kv);
try
{
transfer.Upload(request);
Debug.Log("Transfer Succeeded");
}
catch (AmazonS3Exception e)
{
Debug.Log(e.Message);
}
}
}
void Log(string file_name, byte[] png, Dictionary<string, string> kv)
{
Debug.Log("Access Key: " + kv["AccessKey"]);
Debug.Log("Secret Key: " + kv["SecretKey"]);
Debug.Log("File Name: " + file_name);
Debug.Log("File Size: " + png.Length.ToString());
}
void Capture()
{
var png = MakeScreenShot();
UploadFile(png);
}
// Update is called once per frame
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment