{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from google.cloud import storage\n",
    "from googleapiclient.discovery import build\n",
    "from google.auth import credentials\n",
    "from google.oauth2 import service_account\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = 'zeotap-prod-datapartner-52d31973f15d.json'\n",
    "bucket_name = \"gcs-zt-fansifter-us\"\n",
    "source_file_name = \"fansifter-1.csv\"\n",
    "destination_blob_name = \"fansifter-1.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zeotap-prod-datapartner-52d31973f15d.json\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"]=path\n",
    "\n",
    "print(os.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "storage_client = storage.Client()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "bucket = storage_client.bucket(bucket_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "blob = bucket.blob(destination_blob_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "Forbidden",
     "evalue": "403 POST https://storage.googleapis.com/upload/storage/v1/b/gcs-zt-fansifter-us/o?uploadType=resumable: ('Request failed with status code', 403, 'Expected one of', <HTTPStatus.OK: 200>)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mInvalidResponse\u001b[0m                           Traceback (most recent call last)",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\cloud\\storage\\blob.py\u001b[0m in \u001b[0;36mupload_from_file\u001b[1;34m(self, file_obj, rewind, size, content_type, num_retries, client, predefined_acl, if_generation_match, if_generation_not_match, if_metageneration_match, if_metageneration_not_match)\u001b[0m\n\u001b[0;32m   1566\u001b[0m                 \u001b[0mif_metageneration_match\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1567\u001b[1;33m                 \u001b[0mif_metageneration_not_match\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1568\u001b[0m             )\n",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\cloud\\storage\\blob.py\u001b[0m in \u001b[0;36m_do_upload\u001b[1;34m(self, client, stream, content_type, size, num_retries, predefined_acl, if_generation_match, if_generation_not_match, if_metageneration_match, if_metageneration_not_match)\u001b[0m\n\u001b[0;32m   1432\u001b[0m                 \u001b[0mif_metageneration_match\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1433\u001b[1;33m                 \u001b[0mif_metageneration_not_match\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1434\u001b[0m             )\n",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\cloud\\storage\\blob.py\u001b[0m in \u001b[0;36m_do_resumable_upload\u001b[1;34m(self, client, stream, content_type, size, num_retries, predefined_acl, if_generation_match, if_generation_not_match, if_metageneration_match, if_metageneration_not_match)\u001b[0m\n\u001b[0;32m   1326\u001b[0m             \u001b[0mif_metageneration_match\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mif_metageneration_match\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1327\u001b[1;33m             \u001b[0mif_metageneration_not_match\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mif_metageneration_not_match\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1328\u001b[0m         )\n",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\cloud\\storage\\blob.py\u001b[0m in \u001b[0;36m_initiate_resumable_upload\u001b[1;34m(self, client, stream, content_type, size, num_retries, predefined_acl, extra_headers, chunk_size, if_generation_match, if_generation_not_match, if_metageneration_match, if_metageneration_not_match)\u001b[0m\n\u001b[0;32m   1240\u001b[0m             \u001b[0mtotal_bytes\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msize\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1241\u001b[1;33m             \u001b[0mstream_final\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1242\u001b[0m         )\n",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\resumable_media\\requests\\upload.py\u001b[0m in \u001b[0;36minitiate\u001b[1;34m(self, transport, stream, metadata, content_type, total_bytes, stream_final)\u001b[0m\n\u001b[0;32m    352\u001b[0m         )\n\u001b[1;32m--> 353\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_process_initiate_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    354\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\resumable_media\\_upload.py\u001b[0m in \u001b[0;36m_process_initiate_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    456\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_status_code\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 457\u001b[1;33m             \u001b[0mcallback\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_make_invalid\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    458\u001b[0m         )\n",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\resumable_media\\_helpers.py\u001b[0m in \u001b[0;36mrequire_status_code\u001b[1;34m(response, status_codes, get_status_code, callback)\u001b[0m\n\u001b[0;32m     95\u001b[0m             \u001b[1;34mu\"Expected one of\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 96\u001b[1;33m             \u001b[1;33m*\u001b[0m\u001b[0mstatus_codes\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     97\u001b[0m         )\n",
      "\u001b[1;31mInvalidResponse\u001b[0m: ('Request failed with status code', 403, 'Expected one of', <HTTPStatus.OK: 200>)",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mForbidden\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-9c836f53a082>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mblob\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupload_from_filename\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msource_file_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\cloud\\storage\\blob.py\u001b[0m in \u001b[0;36mupload_from_filename\u001b[1;34m(self, filename, content_type, client, predefined_acl, if_generation_match, if_generation_not_match, if_metageneration_match, if_metageneration_not_match)\u001b[0m\n\u001b[0;32m   1653\u001b[0m                 \u001b[0mif_generation_not_match\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mif_generation_not_match\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1654\u001b[0m                 \u001b[0mif_metageneration_match\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mif_metageneration_match\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1655\u001b[1;33m                 \u001b[0mif_metageneration_not_match\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mif_metageneration_not_match\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1656\u001b[0m             )\n\u001b[0;32m   1657\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\cloud\\storage\\blob.py\u001b[0m in \u001b[0;36mupload_from_file\u001b[1;34m(self, file_obj, rewind, size, content_type, num_retries, client, predefined_acl, if_generation_match, if_generation_not_match, if_metageneration_match, if_metageneration_not_match)\u001b[0m\n\u001b[0;32m   1569\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_set_properties\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcreated_json\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1570\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mresumable_media\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mInvalidResponse\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mexc\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1571\u001b[1;33m             \u001b[0m_raise_from_invalid_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mexc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1572\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1573\u001b[0m     def upload_from_filename(\n",
      "\u001b[1;32mc:\\users\\olv1do\\python-projects\\fansifter\\lib\\site-packages\\google\\cloud\\storage\\blob.py\u001b[0m in \u001b[0;36m_raise_from_invalid_response\u001b[1;34m(error)\u001b[0m\n\u001b[0;32m   2618\u001b[0m     )\n\u001b[0;32m   2619\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2620\u001b[1;33m     \u001b[1;32mraise\u001b[0m \u001b[0mexceptions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfrom_http_status\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2621\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2622\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mForbidden\u001b[0m: 403 POST https://storage.googleapis.com/upload/storage/v1/b/gcs-zt-fansifter-us/o?uploadType=resumable: ('Request failed with status code', 403, 'Expected one of', <HTTPStatus.OK: 200>)"
     ]
    }
   ],
   "source": [
    "blob.upload_from_filename(source_file_name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
