I get following error when I try to upload file to Google Cloud Storage
through servlet using Google Client Library. The wierd part is file
uploading process run okay when I do at local development server, error
happened in deployed version of the app
java.lang.IllegalStateException: No multipart config for servlet
and this is my uploading servlet
import com.google.cloud.storage.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@MultipartConfig()
public class GCSAccessServlet extends HttpServlet {
private static final String BUCKET_NAME = System.getenv("bucketName");
private static Storage storage = null;
@Override
public void init() {
storage = StorageOptions.getDefaultInstance().getService();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
if (BUCKET_NAME.equals("") || BUCKET_NAME == null) {
System.out.println("Bucket is not found (null / empty name)");
}
Bucket bucket = storage.get(BUCKET_NAME);
req.setAttribute("bucketName", bucket.getName());
resp.setContentType("text/html");
RequestDispatcher jsp = req
.getRequestDispatcher("upload.jsp");
jsp.forward(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
IOException,
ServletException {
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
resp.getWriter().print(blob.getMediaLink());
}
}
I couldn't find explanation about this problem.
Thanks.
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-appengine/7bcad7ae-6186-4089-a2c4-e7a29da315e2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.