简体   繁体   English

如何从Google App Engine访问CSV数据?

[英]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.

相关问题 如何使用服务帐户从App Engine访问Google Spreadsheet? - How do I access a Google Spreadsheet from App Engine using a Service Account? 如何从Google App Engine向Android应用发送数据? - How do I send data to an android app from Google App Engine? 如何将数据放入Google应用引擎的数据存储区? - How do I put data into the datastore of Google's app engine? 如何从Google App Engine应用程序运行Google Dataflow管道? - How do I run a Google Dataflow pipeline from a Google App Engine app? 如何在 Google App Engine (Java) 中访问图像中的单个像素? - How do I access individual pixels in an image within Google App Engine (Java)? 如何将本地Google App Engine Python数据存储区复制到本地Google App Engine Java数据存储区? - How do I copy local Google App Engine Python datastore to local Google App Engine Java datastore? 我从GAE上的数据存储中丢失了数据,Google App Engine如何帮助恢复我的数据 - I lost my data from datastore on GAE how Google app engine helps to restore my data 如何将序列化的Java对象从Swing应用程序发送到Google App Engine应用程序,反之亦然? - How do I send serialized java objects from a swing application to a google app engine application and vice versa? 如何使用java从Google应用引擎中的模型中删除字段? - How do I remove a field from a model in google app engine with java? 如何从Google App Engine中的键返回自动生成的数字ID? - How do I return an auto generated numeric ID from the key in google app engine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM