将xml文件上传到Google Cloud Storage时出现错误

时间:2019-01-29 06:55:02

标签: java google-app-engine google-cloud-platform google-cloud-storage

我想将文件上传到Google Cloud Storage,但是出现类似以下错误:

In [1]: from django.core.validators import validate_email

In [2]: faulty_email = 'sid@h.in.'

In [3]: validate_email(faulty_email)
---------------------------------------------------------------------------
ValidationError        Traceback (most recent call last)
<ipython-input-3-bdbbd57d5fe1> in <module>() 
----> 1 validate_email(faulty_email)

/usr/local/lib/python2.7/dist-packages/django/core/validators.pyc in __call__(self, value)
    201             except UnicodeError:
    202                 pass
--> 203             raise ValidationError(self.message, code=self.code)
    204 
    205     def validate_domain_part(self, domain_part):

ValidationError: [u'Enter a valid email address.']

我要上传到Google Cloud Storage的xml文件格式:

java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkNotNull

文件和Google存储桶,我要将文件上传到存储桶。

String fileName =“ data.xml”

String fileBucket =“上传文件”;

<set xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

出现以下错误:

 public static void uploadFile(String fileName, String fileBucket)
            throws IOException {
        final GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
                .initialRetryDelayMillis(10)
                .retryMaxAttempts(10)
                .totalRetryPeriodMillis(50000)//15000
                .build());
        gcsService.createOrReplace(
                new GcsFilename(fileBucket, fileName),
                new GcsFileOptions.Builder().mimeType("application/xml")
                        .acl("public-read")
                        .cacheControl("public, max-age=0").build());
    }

1 个答案:

答案 0 :(得分:1)

To upload a file to the Google Cloud Storage, you need the StorageOptions services. You can see the documentation about Uploading Objects中。

这会将Hello, Cloud Storage!字符串上载到名为blob_name的存储桶中名为bucket的文件中。您只需根据项目的需要更改名称。

上载本地文件之一。创建一个将读取文件数据并将其返回到将数据上传到存储桶的主函数的函数。

我做了一些自我编码,然后编写了以下代码,并成功地将文件与您上面提到的数据一起上传。


读取文件的功能:

它将从本地存储读取文件,例如Cloud Shell,它将返回所有数据。

private String readFile(){
      // The name of the file to open.
        String fileName = "PATH/TO/THE/FILE/THAT/IS/GOING/TO/BE/UPLOADED/FILE_NAME/xml";

        // This will reference one line at a time
        String line = null;
        // This will be the full file after reading
        String output = "";

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = 
                new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
                output = output + line;
            }   

            // Always close files.
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                output = output + "Unable to open file '" + fileName + "'";         
        }
        catch(IOException ex) {
            System.out.println( 
                output = output + "Error reading file '" + fileName + "'";         
        }

      return output;
  }

上传功能:

它将使用将从文件中读取的所有数据并将其上载到存储桶中的新文件。文档代码之间的区别在于调用...readFile().getBytes(UTF_8)...所在的行。代替字符串,我们添加了将返回所有要上传数据的函数。

public String uploadFile(){

        String bucket_name = "BUCKET_NAME";
        String file_name = "PATH/TO/WHERE/THE/FILE/WILL/BE/UPLOADED/FILE_NAME.xml"

        Storage storage = StorageOptions.getDefaultInstance().getService();
        BlobId blobId = BlobId.of(bucket_name, file_name);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
        Blob blob = storage.create(blobInfo, readFile().getBytes(UTF_8));
}