问题
Trying to just list the buckets in my Google Cloud Storage project but can't quite understand why i keep getting the following error:
java.lang.NoSuchMethodError: com.google.api.services.storage.model.Bucket.getIamConfiguration()Lcom/google/api/services/storage/model/Bucket$IamConfiguration;
I'm testing it with the following servlet:
package servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
@WebServlet("/Test")
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
public Test() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Storage storage = StorageOptions.getDefaultInstance().getService();
for (Bucket bucket : storage.list().iterateAll()) { //this line is giving the error
response.getWriter().write(bucket.getName());
}
}
}
In my pom i have:
<dependency>
<groupId>com.google.appengine.tools</groupId>
<artifactId>appengine-gcs-client</artifactId>
<version>0.8</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.64.0</version>
</dependency>
The former dependency isn't required for this specific example but i need it for another part of the application, I suspect this might be where the problem lies. Any ideas what could be going wrong?
回答1:
Since you're using Java: in case you are willing to try google-cloud-nio, it has a listBuckets
method that goes like this:
Page<Bucket> buckets = CloudStorageFileSystem.listBuckets("my-project");
Iterator<Bucket> bucketIterator = buckets.iterateAll();
while (bucketIterator.hasNext()) {
Bucket bucket = bucketIterator.next();
// do something with the bucket
}
回答2:
Found the problem. I was getting a NoSuchMethodError because apparently the library needed wasn't included in the project even though it was specified as a maven dependency. Some kind of mismatch between the libraries on the development server and the libraries on the production server.
The solution in Eclipse was to remove all the revelant Google libraries and add them again by right clicking on the project > Build Path > Configure Build Path... > Add Library... > Google Cloud Platform Libraries > Select "App Engine API" & "Cloud Storage".
来源:https://stackoverflow.com/questions/55033953/listing-buckets-with-google-cloud-storage-resulting-in-nosuchmethoderror-java-a