简体   繁体   中英

Get response code after uploading object to google cloud storage

Hi i am working on a functionality, in it i want to upload the file from the system to the google cloud bucket which is working like a charm but i am unable to figure out a way to get the response code from the cloud bucket when the file has been uploaded succesfully on the cloud. Can someone suggest how can i get the response code back when file is uploaded sucessfully i am attaching the code snippet for a better debug

private void uploadtobucket(List<MultipartFile> selectedfile, String instancekey) {
        // TODO Auto-generated method stub
        String projectid="version1pdf";
        String bucketname="windows10-shivam";
        String temp="temp";
        String path="C:/Users/Shivam/eclipse-workspace/demo.zip_expanded/demo/src/main/resources/version1pdf-996fdd32ec59.json";
        
        List<String> objectname=new ArrayList<String>();
        selectedfile.stream().forEach(selectefile->objectname.add(selectefile.getOriginalFilename()));
        Storage storage=null;
        try {
            storage = StorageOptions.newBuilder().setProjectId(projectid).setCredentials(GoogleCredentials.fromStream(new FileInputStream(path))).build().getService();
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        for(int i=0;i<objectname.size();i++) {
            
            String objectnames="temp/"+objectname.get(i);
            BlobId blobid=BlobId.of(bucketname, objectnames);
            BlobInfo blobinfo=BlobInfo.newBuilder(blobid).build();
            try {
                storage.create(blobinfo, selectedfile.get(i).getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }

any help would be appreciated thanks in advance

According to theofficial documentation on Object Uploads , to upload an object you make a PUT request that is scoped with a bucket name and the object's name, and you put the object data into the request body, using the XML API.

The PUT Object request uses several standard HTTP headers, so this PUT request returns a response header which is set to 200 if the request has been fulfilled successfully, as seen in the example of the above link.

The Cloud Storage XML API is a RESTful interface that lets you manage Cloud Storage data in a programmatic way. As a RESTful API, it relies on method information and scoping information to define the operations to perform. For more information about XML API and GCS you can visit thethis link.

Additionally,here you may find further information on the HTTP headers and query string parameters for XML API andhere is a summary of the request methods for the XML API.

Furthermore, keep in mind that if you need to use HTML forms (usually through a web browser) to upload objects, try using POST object instead of PUT.

I hope this information helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM