【问题标题】:local image upload not showing on Google App Engine本地图片上传未显示在 Google App Engine 上
【发布时间】:2019-09-23 13:28:18
【问题描述】:

这是我在服务器端的上传功能:

private static final int BUFFER_SIZE = 2 * 1024 * 1024;
public void processRequest(HttpServletRequest request)
        throws ServletException, IOException {

    // multipart request
    final Part filePart = request.getPart("file");
    // some function to get the filename from part
    final String name = getFileName(filePart);
    // some function to convert the filename to GcsFilename
    final GcsFilename fileName = getFileNameGCS(name);
    //method to copy file taken from official documentation
    GcsFileOptions instance = GcsFileOptions.getDefaultInstance();
    GcsOutputChannel outputChannel = gcsService.createOrReplace(fileName, instance);
    copy(filePart.getInputStream(), Channels.newOutputStream(outputChannel));

}

private void copy(InputStream input, OutputStream output) throws IOException {
        try {
          byte[] buffer = new byte[BUFFER_SIZE];
          int bytesRead = input.read(buffer);
          while (bytesRead != -1) {
            output.write(buffer, 0, bytesRead);
            bytesRead = input.read(buffer);
          }
        } finally {
          input.close();
          output.close();
        }
    }

这是我在客户端(Angular2)album.component.ts 上的帖子功能:

  uploadAlbum(index:number){
    const formData = new FormData();
    formData.append('file', this.targets[index].file, '/gcs/my-bucket/' + index +'/cover/cover.jpg');
    this.http.post('/api/photos',formData, {responseType: 'text'}).subscribe(
      (data: any) => {
        console.log(data)
      },
       err => {
         console.log(err)
        }
      );
  }

现在我使用这种方法来检索我的本地文件的公共 url(App Engine Google Cloud Storage 模拟服务器上传并将其放置在本地空间中)

示例代码:

...
BlobKey blobKey = blobStore.createGsBlobKey("/gs/" + bucket + "/" + listFilenames.get(i));
log.info("/gs/" + bucket + "/" + listFilenames.get(i));
ImagesService imagesService = ImagesServiceFactory.getImagesService();
String url = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey));
jsonObj.put("path", url);
json.put(jsonObj);
...

基本上我在函数的另一部分所做的是解析所有文件并过滤我感兴趣的文件。

它的工作原理是我得到带有encoded_gs_key 的 URL,但什么都没有显示。 我的上传有问题吗?即使在指定位置找不到文件,BlobStore 也会提供链接吗? 另一件事是,即使我的文件夹和图像不同(0/cover/cover.jpg 和例如1/cover/cover.jpg 如此不同的根文件夹、不同的图像但相同的文件名),我总是得到相同的 encoded_gs_key 链接

【问题讨论】:

  • 你是如何模拟本地云存储的?您是在遵循快速入门还是指南?另外,您能否提供代码的所有部分以便重现?
  • @TasosZG 感谢您的评论。我正在使用这个tutorial。我不是在模拟本地云存储,它是开发模式下的 appengine,它通过上传到本地空间来模拟在线上传。还有大部分重要的代码都在这里我可以添加客户端(html/js)虽然
  • 您还可以通过转到http://localhost:8080/_ah/admin 来查看模拟(数据存储)存储。您可以列出每个 Kind 的实体,每个 Kind 代表一个“假桶”。还有一种额外的类型将键映射到encoded_gs_keys。你能检查一下你上传的图片有没有?
  • 当我运行mvn appengine:devserver 来启动本地开发服务器时,它给了我“模块实例默认运行在http://localhost:8080/”和“管理控制台运行在http://localhost:8080/_ah/admin”。我在 Cloud Shell 上运行 this sample
  • 太棒了!很高兴你解决了它!

标签: java image google-app-engine google-cloud-storage


【解决方案1】:

现在我明白发生了什么。我有一个连接过滤器,它让我只能通过这条路径/api* 我已经删除它并且事情似乎工作得更好:

原来的 connexionFilter 类:

public class connexionFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String path = ((HttpServletRequest) request).getRequestURI();

        if(path.startsWith("/api"))
        {
            chain.doFilter(req, resp);
        }
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-07
    • 2012-09-05
    • 2015-02-07
    • 2013-09-14
    • 2013-07-13
    • 1970-01-01
    • 2010-09-14
    • 2013-08-25
    相关资源
    最近更新 更多