Last active
December 20, 2015 01:28
-
-
Save sinmetal/6048795 to your computer and use it in GitHub Desktop.
Google Cloud Storage Client Libraryを適当に使ってみたサンプル。 with slim3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.sinmetal.appengine.sample.model.gcs; | |
import org.slim3.datastore.Attribute; | |
import org.slim3.datastore.Datastore; | |
import org.slim3.datastore.Model; | |
import com.google.appengine.api.datastore.Key; | |
@Model(schemaVersionName = "schemaVersion", schemaVersion = 1) | |
public class FileInfo { | |
@Attribute(primaryKey = true) | |
Key key; | |
String gsBucketName; | |
String fileName; | |
String remarks; | |
public static Key createKey(String bucketName, String fileName) { | |
return Datastore.createKey(FileInfo.class, String.format("%s-_-%s", bucketName, fileName)); | |
} | |
/** | |
* @return the key | |
* @category accessor | |
*/ | |
public Key getKey() { | |
return key; | |
} | |
/** | |
* @param key the key to set | |
* @category accessor | |
*/ | |
public void setKey(Key key) { | |
this.key = key; | |
} | |
/** | |
* @return the gsBucketName | |
* @category accessor | |
*/ | |
public String getGsBucketName() { | |
return gsBucketName; | |
} | |
/** | |
* @param gsBucketName the gsBucketName to set | |
* @category accessor | |
*/ | |
public void setGsBucketName(String gsBucketName) { | |
this.gsBucketName = gsBucketName; | |
} | |
/** | |
* @return the fileName | |
* @category accessor | |
*/ | |
public String getFileName() { | |
return fileName; | |
} | |
/** | |
* @param fileName the fileName to set | |
* @category accessor | |
*/ | |
public void setFileName(String fileName) { | |
this.fileName = fileName; | |
} | |
/** | |
* @return the remarks | |
* @category accessor | |
*/ | |
public String getRemarks() { | |
return remarks; | |
} | |
/** | |
* @param remarks the remarks to set | |
* @category accessor | |
*/ | |
public void setRemarks(String remarks) { | |
this.remarks = remarks; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.sinmetal.appengine.sample.service.gcs; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.nio.channels.Channels; | |
import org.sinmetal.appengine.sample.model.gcs.FileInfo; | |
import org.slim3.controller.upload.FileItem; | |
import org.slim3.datastore.Datastore; | |
import com.google.appengine.api.datastore.Key; | |
import com.google.appengine.api.datastore.Transaction; | |
import com.google.appengine.tools.cloudstorage.GcsFileOptions; | |
import com.google.appengine.tools.cloudstorage.GcsFilename; | |
import com.google.appengine.tools.cloudstorage.GcsOutputChannel; | |
import com.google.appengine.tools.cloudstorage.GcsService; | |
import com.google.appengine.tools.cloudstorage.GcsServiceFactory; | |
public class FileService { | |
public static Key entry(String bucketName, String remarks, FileItem fileItem) | |
throws IOException { | |
final GcsService gcsService = GcsServiceFactory.createGcsService(); | |
Transaction tx = null; | |
try { | |
tx = Datastore.beginTransaction(); | |
final GcsFilename gcsFilename = new GcsFilename(bucketName, fileItem.getFileName()); | |
GcsOutputChannel outputChannel = | |
gcsService.createOrReplace(gcsFilename, GcsFileOptions.getDefaultInstance()); | |
try (OutputStream outputStream = Channels.newOutputStream(outputChannel)) { | |
outputStream.write(fileItem.getData()); | |
} | |
FileInfo fileInfo = new FileInfo(); | |
fileInfo.setKey(FileInfo.createKey(bucketName, fileItem.getFileName())); | |
fileInfo.setGsBucketName(bucketName); | |
fileInfo.setFileName(fileItem.getFileName()); | |
fileInfo.setRemarks(remarks); | |
Key key = Datastore.put(fileInfo); | |
tx.commit(); | |
return key; | |
} finally { | |
if (tx != null && tx.isActive()) { | |
tx.rollback(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>GoogleCloudStorageSample</title> | |
<meta charset="utf-8"> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
<script> | |
$(document).ready(function () { | |
$("#create").on("click", function () { | |
var $form = $("#entryForm"); | |
var formData = new FormData($form[0]); | |
var promise = $.ajax({ | |
type: "POST", | |
url: "/gcs/upload", | |
processData: false, | |
contentType: false, | |
data: formData | |
}); | |
promise | |
.success(function () { | |
alert("作成しました"); | |
}) | |
.error(function () { | |
alert("作成に失敗しました"); | |
}); | |
return false; | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<form id="entryForm"> | |
画像:<input name="file" type="file" enctype="multipart/form-data"> | |
<input name="remarks" type="text" placeholder="備考"> | |
<button id="create">作成する</button> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.sinmetal.appengine.sample.controller.gcs; | |
import org.sinmetal.appengine.sample.service.gcs.FileService; | |
import org.slim3.controller.Controller; | |
import org.slim3.controller.Navigation; | |
import org.slim3.controller.upload.FileItem; | |
/** | |
* @author sinmetal | |
*/ | |
public class UploadController extends Controller { | |
@Override | |
protected Navigation run() throws Exception { | |
final String remarks = asString("remarks"); | |
final FileItem fileItem = requestScope("file"); | |
FileService.entry("your-bucket-name", remarks, fileItem); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment