Ich hatte einige Probleme mit der Konfiguration von Google Cloud-Speicher und kann für das Leben von mir nicht herausfinden, was das Problem ist.public class GoogleCloudStorageServiceImpl implements StorageService {
private static final Logger LOGGER = LogManager.getLogger(GoogleCloudStorageServiceImpl.class);
private final Storage storage;
public GoogleCloudStorageServiceImpl() {
this.storage = StorageOptions.newBuilder()
.setProjectId("redacted")
.build().getService();
}
@Override
public void store(File file) {
LOGGER.info("Attempting to store file: {}", file.getAbsolutePath());
if (this.fileExistsInGcs(file, storage)) {
LOGGER.warn("File already exists in GCS: {}", file.getName());
throw new TechException("Error: File already exists in GCS");
}
byte[] bytes = this.convertFileToByteArray(file);
BlobInfo blobInfo = BlobInfo.newBuilder("redacted", file.getName()).build();
storage.create(blobInfo, bytes);
LOGGER.info("Successfully uploaded file '{}' to bucket '{}'", file.getName(), "redacted");
}
// helper methods
}
< /code>
und nach dem Schreiben einer Hauptmethode innerhalb des ServiceImpl und der Erstellung einer neuen Klasse außerhalb des ServiceImpl erhalte ich beim Ausführen im normalen Modus immer eine Verbindung, die eine Ausnahme abgelehnt hat. < /p>
Exception in thread "main" com.google.cloud.storage.StorageException: Error getting access token for service account: Connection refused: connect, iss: redacted
at com.google.cloud.storage.StorageException.translate(StorageException.java:230)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:360)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.get(HttpStorageRpc.java:598)
at com.google.cloud.storage.StorageImpl.lambda$internalGetBlob$70(StorageImpl.java:1678)
at com.google.cloud.storage.StorageImpl$$Lambda$149/412925308.call(Unknown Source)
at com.google.cloud.storage.Retrying$DefaultRetrier.run(Retrying.java:165)
at com.google.cloud.storage.Retrying$HttpRetrier.run(Retrying.java:206)
at com.google.cloud.storage.StorageImpl.run(StorageImpl.java:1619)
< /code>
File file1 = File.createTempFile("test-upload", ".txt");
try (FileWriter writer = new FileWriter(file1)) {
writer.write("This is a test file for upload.");
}
storageService.store(file1);
< /code>
Ich habe mit dem neuesten Oracle JDK, Java 1.8 Oracle und Java 1.8 Coretto versucht, der keiner von beiden einen Unterschied gemacht hat. Es läuft jedoch im Debug -Modus vollkommen in Ordnung. Ich habe auch Thread.sleep () ohne Glück ausprobiert. class = "Lang-Java PrettyPrint-Override">public class GoogleCloudStorageServiceImpl {
public static void main(String... args) throws IOException {
// The ID of your GCP project
String projectId = "redacted";
// The ID of your GCS bucket
String bucketName = "redacted";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
File file1 = File.createTempFile("test-upload", ".txt");
try (FileWriter writer = new FileWriter(file1)) {
writer.write("This is a test file for upload.");
}
byte[] file = Files.readAllBytes(file1.toPath());
BlobInfo blobInfo = BlobInfo.newBuilder("redacted", file1.getName()).build();
storage.create(blobInfo, file);
Page blobs = storage.list(bucketName);
for (Blob blob : blobs.iterateAll()) {
System.out.println(blob.getName());
System.out.println(new String(blob.getContent()));
}
}
}
< /code>
Jede Hilfe wäre geschätzt!
Fehler beim Erhalten von Zugriffstoken für das Servicekonto: Verbindung abgelehnt: Connect, ISS: [...] ⇐ Java
-
- Similar Topics
- Replies
- Views
- Last post
-
-
So erhalten Sie ein Microsoft GraphServiceClient in Java mit einem Zugriffstoken
by Anonymous » » in Java - 0 Replies
- 6 Views
-
Last post by Anonymous
-