[英]How do I access CSV data from Google App Engine?
I have a simple web app that lets users upload a CSV to GAE blob storage. 我有一个简单的网络应用,可让用户将CSV上传到GAE blob存储。 I am trying to pass that data to a google analytics API method but I cannot work out how.
我正在尝试将该数据传递给Google Analytics(分析)API方法,但无法解决。 Following the tutorial doc, I only seem to be able to return the file in the response header.
遵循教程文档之后,我似乎只能返回响应头中的文件。
Is there a way to pass the blob directly to the method as a file? 有没有办法将blob作为文件直接传递给方法?
Alternatively is there a way to upload a blob and then map the data to Cloud Storage? 或者,是否可以上传Blob,然后将数据映射到Cloud Storage?
If you follow the code here https://cloud.google.com/appengine/docs/java/blobstore/ it will guide you through setting up the upload URL and providing a callback URL, etc... When the file is uploaded and your callback URL is called you need to remember the blob key, you can save in the datastore, keep it in the session or whatever as you need it to read the blob back from the Datastore. 如果您按照此处的代码https://cloud.google.com/appengine/docs/java/blobstore/进行操作 ,它将指导您设置上传URL并提供回调URL,等等。您的回调URL被称为,您需要记住blob键,您可以将其保存在数据存储区中,将其保留在会话中,也可以根据需要使用它来从数据存储区读回blob。
The code below should help, in particular the method your interested in is getBlobAsString or you can simply use the blobStream ( BlobstoreInputStream blobStream = new BlobstoreInputStream(blobKey); ) to pass the file content around as an InputStream 下面的代码应该会有所帮助,特别是您感兴趣的方法是getBlobAsString,或者您可以简单地使用blobStream( BlobstoreInputStream blobStream = new BlobstoreInputStream(blobKey); )将文件内容作为InputStream传递给周围
In your servlet handling the upload callback 在您的Servlet中处理上传回调
import org.apache.commons.io.IOUtils;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreInputStream;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
public class Upload extends HttpServlet {
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobKey = blobs.get("myFile");
if (blobKey != null) {
String keyString = blobKey.getKeyString();
// you can store keyString in the datastore or whatever
// if you want the call the analytics API then you need the blob as string
String csv = getBlobAsString(keyString);
// you can do with csv whatever you like, convert it as an array, etc... then pass it
// over to the analytic API
}
}
public static String getBlobAsString(String keyString) throws IOException {
BlobKey blobKey = new BlobKey(keyString);
BlobstoreInputStream blobStream = new BlobstoreInputStream(blobKey);
return IOUtils.toString(blobStream, "UTF-8");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.