1

在 GAE 中上传文件时,我可以使用如下代码,

 //here creating a blob store service
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
//here declaring the datastore service
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//creating entity
   Entity emp = new Entity("Employee");
//set the property
   emp.setProperty("First Name", fname);
   emp.setProperty("Last Name", lname);
//storing the data in GAE
   datastore.put(Employee);

现在,如果我需要在 Google 云存储中进行相同的文件上传,我可以设置存储桶并打开通道,如下所示,

//Creating file service
FileService fileService = FileServiceFactory.getFileService();

//setting bucket name,object name & type
GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
       .setBucket(BUCKETNAME)
       .setKey(FILENAME)
       .setMimeType("text/html")
       .setAcl("public_read")
       .addUserMetadata("myfield1", "my field value");

//creating the new writable file 
 AppEngineFile writableFile =fileService.createNewGSFile(optionsBuilder.build());
//opening the lock and writing channel
boolean lock = false;
FileWriteChannel writeChannel = fileService.openWriteChannel(writableFile, lock);

在此之后,我不知道如何将文件存储在 Google 云存储中。我参考了这个开发者页面谷歌云存储 Java API 概述,但它只显示读取和写入对象而不是上传文件。

请建议我在 Google 云存储中上传文件的想法。

感谢您的帮助。

4

1 回答 1

2

像这样的东西:

UploadOptions uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName("bucket");
String uploadUrl = blobstoreService.createUploadUrl("/on_upload_success", uploadOptions));

然后发布到该网址。但是,然后在“/on_upload_success”处理程序中,您从 GS 获取文件的 BlobKey,而不是文件名(由于此错误http://code.google.com/p/googleappengine/issues/detail?id=8337)。因此,您只能使用 blobkey 从 GAE 读取该数据。

于 2012-12-26T10:33:25.280 回答