借助使用Google文档Writing files to a blob store
我可以通过以下方式将文件存储在Google Cloud中,
//My servlet Class
private StorageService storage = new StorageService();
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime, fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1;
storage.init(fileName, mime); // Calling my Java Class Here
while ((readBytes1 = is1.read(b1)) != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
storage.storeFile(b1, readBytes1);}
writeChannel.closeFinally();
我有一个存储java类,用于将数据存储在此处的Google云中,
//My Storage Class
public class StorageService {
public static final String BUCKET_NAME = "MyBucket";
public void init(String fileName, String mime) throws Exception {
GSFileOptionsBuilder builder = new GSFileOptionsBuilder().setAcl("public_read").setBucket(BUCKET_NAME).setKey(fileName).setMimeType(mime);
AppEngineFile writableFile = fileService.createNewGSFile(builder.build());
boolean lock = true;
writeChannel = fileService.openWriteChannel(writableFile, lock);
bos = new BufferedOutputStream(Channels.newOutputStream(writeChannel));
public void storeFile(byte[] b, int readSize) throws Exception {
bos.write(b,0,readSize);
bos.flush();
}}
从上面的参考资料中我可以直接将文件存储在Google云中。但我需要使用File服务将这些文件存储为blob值。
任何人都可以建议我一个想法。
您的帮助将不胜感激。
答案 0 :(得分:0)
您是否在Google API控制台项目中添加了appengine服务帐户? 如果您还没有完成,则应准备Google API控制台项目。