簡體   English   中英

獲取 java.net.ConnectException: Connection refused (Connection refused) while creating object using fake-gcs-server 圖像

[英]Getting java.net.ConnectException: Connection refused (Connection refused) while creating object using fake-gcs-server image

我在嘗試編寫 junit 以使用fake-gcs-server圖像在 gcs 中創建 object 時收到java.net.ConnectException:connection refused 錯誤。 請找到以下代碼。 Bucket 名稱為 test and consider,它已經創建。

@TempDir
private static File         directory;

private static final GenericContainer<?> GCS_CONTAINER = new GenericContainer<>(DockerImageName.parse("fsouza/fake-gcs-server:1.33.1"))
        .withExposedPorts(4443).withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint(
                "/bin/fake-gcs-server",
                "-scheme", "http"
        ));

String fakeGcsExternalUrl = "http://" + GCS_CONTAINER.getContainerIpAddress() + ":" + GCS_CONTAINER.getFirstMappedPort();

private static final Storage storage = new Storage.Builder(getTransport(), GsonFactory.getDefaultInstance(), null).setRootUrl(fakeGcsExternalUrl).setApplicationName("test").build();

void test() {

    final File localFile1 = new File(directory.getAbsolutePath() + File.separator + "testFile.txt");
    localFile1.createNewFile();

    try (final FileWriter fileWriter = new FileWriter(localFile1.getPath())) {
        fileWriter.write("Test gs file content");
    }


    final InputStream stream = FileUtils.openInputStream(localFile1);

    Path path = localFile1.toPath();
    String contentType = Files.probeContentType(path);
  uploadFile("test", "/sampleFiles/newFile.txt", contentType, stream, null);
}

public String uploadFile(final Storage storage, final String bucketName, final String filePath,
                         final String contentType, final InputStream inputStream, final Map<String, String> metadata)
        throws IOException {
    final InputStreamContent contentStream = new InputStreamContent(contentType, inputStream);
    final StorageObject objectMetadata = new StorageObject().setName(filePath);
    objectMetadata.setMetadata(GoogleLabels.manageLabels(metadata));
    final Storage.Objects.Insert insertRequest = storage.objects().insert(bucketName, objectMetadata,
            contentStream);
    return insertRequest.execute().getName();
}

此問題可能與您沒有將 fake-gcs-server 的外部 URL 屬性設置為容器地址有關。 確保遵循官方倉庫 https://github.com/fsouza/fake-gcs-server/blob/cf3fcb083e19553636419818e29f84825bd1e13c/examples/java/README.md中的指南,特別是執行以下代碼:

private static void updateExternalUrlWithContainerUrl(String fakeGcsExternalUrl) throws Exception {
  String modifyExternalUrlRequestUri = fakeGcsExternalUrl + "/_internal/config";
  String updateExternalUrlJson = "{"
      + "\"externalUrl\": \"" + fakeGcsExternalUrl + "\""
      + "}";

  HttpRequest req = HttpRequest.newBuilder()
      .uri(URI.create(modifyExternalUrlRequestUri))
      .header("Content-Type", "application/json")
      .PUT(BodyPublishers.ofString(updateExternalUrlJson))
      .build();
  HttpResponse<Void> response = HttpClient.newBuilder().build()
      .send(req, BodyHandlers.discarding());

  if (response.statusCode() != 200) {
      throw new RuntimeException(
          "error updating fake-gcs-server with external url, response status code " + response.statusCode() + " != 200");
  }
}

在使用容器之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM