上传:uploadData

需要授权

上传用于自定义数据源的数据。 立即试用查看示例

此方法支持 /upload URI,并接受具备以下特征的已上传媒体:

  • 最大文件大小:1 GB
  • 可接受的媒体 MIME 类型application/octet-stream

除了标准参数之外,此方法还支持参数表中列出的参数。

请求

HTTP 请求

POST https://www.googleapis.com/upload/analytics/v3/management/accounts/accountId/webproperties/webPropertyId/customDataSources/customDataSourceId/uploads

参数

参数名称 说明
路径参数
accountId string 与上传相关联的帐户 ID。
customDataSourceId string 要上传的数据所属的自定义数据源 ID。
webPropertyId string 与上传相关联的网络媒体资源 UA 字符串。
必需查询参数
uploadType string 针对 /upload URI 的上传请求的上传类型。可接受的值为:
  • media - 简单上传。上传媒体数据。
  • resumable - 可恢复的上传。以可恢复方式上传文件,使用至少包含两个请求的一系列请求。

授权

此请求至少需要获得以下任一范围的授权(详细了解身份验证和授权)。

范围
https://www.googleapis.com/auth/analytics
https://www.googleapis.com/auth/analytics.edit

请求正文

请勿使用此方法提供请求正文。

响应

如果成功,此方法将在响应正文中返回一个“上传”资源

示例

备注:此方法的代码示例并未列出所有受支持的编程语言(请参阅客户端库页面,查看受支持的语言列表)。

Java

使用 Java 客户端库

/*
 * Note: This code assumes you have an authorized Analytics service object.
 * See the Data Import Developer Guide for details.
 */


// This request uploads a file for the authorized user.
File file = new File("data.csv");
InputStreamContent mediaContent = new InputStreamContent("application/octet-stream",
    new FileInputStream(file));
mediaContent.setLength(file.length());
try {
  analytics.management().uploads().uploadData("123456",
      "UA-123456-1", "122333444455555", mediaContent).execute();
} catch (GoogleJsonResponseException e) {
  System.err.println("There was a service error: "
      + e.getDetails().getCode() + " : "
      + e.getDetails().getMessage());
}

PHP

使用 PHP 客户端库

/**
 * Note: This code assumes you have an authorized Analytics service object.
 * See the Data Import Developer Guide for details.
 */

/**
 * This request uploads a file to a custom data source.
 */
try {
  $analytics->management_uploads->uploadData(
      '123456',
      'UA-123456-1',
      '122333444455555',
      array('data' => file_get_contents('example.csv'),
            'mimeType' => 'application/octet-stream',
            'uploadType' => 'media'));

} catch (apiServiceException $e) {
  print 'There was an Analytics API service error '
      . $e->getCode() . ':' . $e->getMessage();

} catch (apiException $e) {
  print 'There was a general API error '
      . $e->getCode() . ':' . $e->getMessage();
}

Python

使用 Python 客户端库

# Note: This code assumes you have an authorized Analytics service object.
# See the Data Import Developer Guide for details.


# This request uploads a file custom_data.csv to a particular customDataSource.
# Note that this example makes use of the MediaFileUpload Object from the
# apiclient.http module.
from apiclient.http import MediaFileUpload
try:
  media = MediaFileUpload('custom_data.csv',
                          mimetype='application/octet-stream',
                          resumable=False)
  daily_upload = analytics.management().uploads().uploadData(
      accountId='123456',
      webPropertyId='UA-123456-1',
      customDataSourceId='9876654321',
      media_body=media).execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error

except HttpError, error:
  # Handle API errors.
  print ('There was an API error : %s : %s' %
         (error.resp.status, error.resp.reason))

JavaScript

使用 JavaScript 客户端库

/*
 * Note: This code assumes you have an authorized Analytics client object.
 * See the Data Import Developer Guide for details.
 */

/*
 * This request uploads a file for the authorized user.
 */
function uploadData(fileData) {
  const boundary = '-------314159265358979323846';
  const delimiter = "\r\n--" + boundary + "\r\n";
  const close_delim = "\r\n--" + boundary + "--";

  var contentType = 'application/octet-stream'

  var reader = new FileReader();
  reader.readAsBinaryString(fileData);
  reader.onload = function(e) {
    var contentType = 'application/octet-stream';
    var metadata = {
      'title': fileData.name,
      'mimeType': contentType
    };

    var base64Data = btoa(reader.result);
    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: ' + contentType + '\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        '\r\n' +
        base64Data +
        close_delim;

    var request = gapi.client.request({
      'path': 'upload/analytics/v3/management/accounts/123456/webproperties/UA-123456-1/customDataSources/ABCDEFG123abcDEF123/uploads',
      'method': 'POST',
      'params': {'uploadType': 'multipart'},
      'headers': {
        'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
      },
      'body': multipartRequestBody
    });
  request.execute(function (response) { // Handle the response. });
  }
}