GAE中的错误502使用云存储灵活

时间:2017-04-05 14:16:08

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

我使用GAE Flexible创建了一个应用程序。我可以毫无问题地部署和使用该应用程序。然后,我使用JSON service_account将上传文件添加到Google云端存储,并且它在localhost中正常运行。我已经部署它没有任何错误但是当部署完成后,我尝试浏览我的应用程序时看到以下错误502 server error

Error: Server Error
The server encountered a temporary error and could not complete your request.
Please try again in 30 seconds.

我的应用程序是非常简单的应用程序,没有很多请求,所以我不需要退避。 在从控制台登录时,我看到以下错误:

[error] 32#32: *9771 connect() failed (113: No route to host) while connecting to upstream, client: 130.211.3.171, server: , request: "GET /_ah/health HTTP/1.1", upstream: "http://172.18.0.2:8080/_ah/health", host: "10.128.0.2"

这是我的课程,将图像放入云端存储:

    @SuppressWarnings("serial")
@WebServlet(name = "upload", value = "/upload")
@MultipartConfig()
public class UploadServlet extends HttpServlet {

  private static final Logger logger = LoggerFactory.getLogger(UploadServlet.class); 

  private static final String BUCKET_NAME ="myBUCKET_NAME";// System.getenv("BUCKET_NAME");
  private static Storage storage = null;

  @Override
  public void init() {
      try {
        logger.debug("bucket name: " + BUCKET_NAME  );
        InputStream is = getClass().getResourceAsStream("/Shelfcheck-9faaa7cbc5a5.json");
        storage = StorageOptions.newBuilder()
                  .setCredentials(ServiceAccountCredentials.fromStream(is))
                  .build()
                  .getService();
        logger.debug("set credential correctly!"  );

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }          

//  Storage  storage_service = StorageOptions.defaultInstance().service();
//  Iterator<Bucket> bucketIterator = storage_service.list().iterateAll();
//  while (bucketIterator.hasNext()) {
//    System.out.println(bucketIterator.next());
//  }


//    storage = StorageOptions.getDefaultInstance().getService();
  }

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,
      ServletException {

      logger.debug("Start uploading the files...");

    final Part filePart = req.getPart("file");
    final String fileName = filePart.getSubmittedFileName();

    // Modify access list to allow all users with link to read file
    List<Acl> acls = new ArrayList<>();
    acls.add(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
    // the inputstream is closed by default, so we don't need to close it here
    Blob blob =
        storage.create(
            BlobInfo.newBuilder(BUCKET_NAME, fileName).setAcl(acls).build(),
            filePart.getInputStream());

    // return the public download link
    logger.debug(blob.getMediaLink());

    resp.getWriter().print(blob.getMediaLink());
    logger.debug(resp.toString());
    logger.debug(blob.getMediaLink());
  }
}

任何人都知道如何解决这个问题? 感谢

0 个答案:

没有答案