<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: In a Notebook, download shapefile from FTP and update existing feature service in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1328246#M8997</link>
    <description>&lt;P&gt;I don't use ArcGIS Notebooks much these days, so my assumption is there's some problem you're running into writing the zips to disk. I think you may be able to accomplish this in memory using a BytesIO object. I don't have immediate access to an FTP so I'm going to approximate this workflow using a web request, but the logic is pretty much the same.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a working example of how to do the initial publish:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests
import io
from arcgis import GIS

zip_url = "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_cd116_20m.zip"
r = requests.get(zip_url)
f = io.BytesIO()
f.write(r.content)

gis = GIS("https://arcgis.com/", "username", "pass")
item_properties = {
    "type": "Shapefile", 
    "title": "test", 
    "fileName": "virtual_zip.zip"
}
shpfile = gis.content.add(item_properties, f)
published_service = shpfile.publish()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can overwrite like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests
import io
from arcgis import GIS
from arcgis.features import FeatureLayerCollection

zip_url = "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_cd116_20m.zip"
r = requests.get(zip_url)
f = io.BytesIO()
f.write(r.content)

gis = GIS("https://arcgis.com/", "username", "pass")
item_id = "d44e3b2892a54f25a11def293724acbc"
item = gis.content.get(item_id)
flc = FeatureLayerCollection.fromitem(item)
flc.manager.overwrite(f)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your case, I think the for loop would change to something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;f = io.BytesIO()
ftp.retrbinary("RETR "+ zipfile, f.write)
f.seek(0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Sep 2023 13:30:07 GMT</pubDate>
    <dc:creator>EarlMedina</dc:creator>
    <dc:date>2023-09-13T13:30:07Z</dc:date>
    <item>
      <title>In a Notebook, download shapefile from FTP and update existing feature service</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1327576#M8993</link>
      <description>&lt;P&gt;I have a stand-alone Python script to download a zipped shapefile from an FTP site and upload to ArcGIS Online update an existing hosted feature service. Here's the simplified version --&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;#Set folder to download to&lt;BR /&gt;folder = 'C:\\myftpdownloads\\'&lt;BR /&gt;out_path = os.path.join(folder)&lt;/P&gt;&lt;P&gt;#FTP logon&lt;BR /&gt;ftp = FTP('ftp.myftp.com')&lt;BR /&gt;ftp.login('FTPusername', 'FTPpassword')&lt;/P&gt;&lt;P&gt;#Get a list of all files in the folder - for simplicity, let's pretend there is only ever 1 zip file there&lt;BR /&gt;files = ftp.nlst()&lt;/P&gt;&lt;P&gt;#Loop through the files to download&lt;BR /&gt;for zipfile in files:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;with open(os.path.join(out_path, zipfile), 'wb') as local_file:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;ftp.retrbinary('RETR '+ zipfile, local_file.write)&lt;/P&gt;&lt;P&gt;#Upload and update existing hosted feature service in ArcGIS Online&lt;BR /&gt;gis = GIS("&lt;A href="https://arcgis.com/" target="_blank"&gt;https://arcgis.com/&lt;/A&gt;", "AGOUser", "AGOpassword")&lt;BR /&gt;item = gis.content.get('123456789mycontentid987654321')&lt;BR /&gt;# Call the update method to replace/overwrite it with the zip file from disk&lt;BR /&gt;item.update({}, zipfile)&lt;BR /&gt;# Update hosted feature layer&lt;BR /&gt;# Make sure to uncheck editing in Settings&lt;BR /&gt;item.publish(overwrite=True,file_type='shapefile')&lt;BR /&gt;&lt;BR /&gt;I want to migrate this to an ArcGIS Online Notebook so that it can be scheduled and without having a dedicated machine running ArcGIS Pro and always on.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I see good articles on &lt;A href="https://enterprise.arcgis.com/en/notebook/latest/use/windows/upload-datasets-to-use-with-arcpy.htm" target="_self"&gt;Uploading datasets to use with ArcPy,&lt;/A&gt;&amp;nbsp;&lt;A href="https://www.arcgis.com/home/item.html?id=fe8a61ca94c54e6e8e62c2faed0b68cf" target="_self"&gt;Using ArcPy in ArcGIS Notebooks&lt;/A&gt;, and&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/attempting-to-upload-files-to-notebook-files-using/td-p/1288227" target="_self"&gt;Attempting to Upload Files to ArcGIS Notebooks Programmatically&lt;/A&gt;&amp;nbsp;-- but I can't find the exact right tutorial or post that puts it all together...&lt;BR /&gt;&lt;BR /&gt;I need to programmatically download from an FTP site and upload directly ArcGIS Notebook.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Would anyone have ideas? Is this even possible?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2023 04:31:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1327576#M8993</guid>
      <dc:creator>rdbutger</dc:creator>
      <dc:date>2023-09-12T04:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: In a Notebook, download shapefile from FTP and update existing feature service</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1328246#M8997</link>
      <description>&lt;P&gt;I don't use ArcGIS Notebooks much these days, so my assumption is there's some problem you're running into writing the zips to disk. I think you may be able to accomplish this in memory using a BytesIO object. I don't have immediate access to an FTP so I'm going to approximate this workflow using a web request, but the logic is pretty much the same.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a working example of how to do the initial publish:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests
import io
from arcgis import GIS

zip_url = "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_cd116_20m.zip"
r = requests.get(zip_url)
f = io.BytesIO()
f.write(r.content)

gis = GIS("https://arcgis.com/", "username", "pass")
item_properties = {
    "type": "Shapefile", 
    "title": "test", 
    "fileName": "virtual_zip.zip"
}
shpfile = gis.content.add(item_properties, f)
published_service = shpfile.publish()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can overwrite like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests
import io
from arcgis import GIS
from arcgis.features import FeatureLayerCollection

zip_url = "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_cd116_20m.zip"
r = requests.get(zip_url)
f = io.BytesIO()
f.write(r.content)

gis = GIS("https://arcgis.com/", "username", "pass")
item_id = "d44e3b2892a54f25a11def293724acbc"
item = gis.content.get(item_id)
flc = FeatureLayerCollection.fromitem(item)
flc.manager.overwrite(f)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your case, I think the for loop would change to something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;f = io.BytesIO()
ftp.retrbinary("RETR "+ zipfile, f.write)
f.seek(0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Sep 2023 13:30:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1328246#M8997</guid>
      <dc:creator>EarlMedina</dc:creator>
      <dc:date>2023-09-13T13:30:07Z</dc:date>
    </item>
    <item>
      <title>Re: In a Notebook, download shapefile from FTP and update existing feature service</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1328895#M9001</link>
      <description>&lt;P&gt;Thanks, that looks promising. I will get it a shot and let you know how it goes.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 21:01:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1328895#M9001</guid>
      <dc:creator>RichardButgereit</dc:creator>
      <dc:date>2023-09-14T21:01:11Z</dc:date>
    </item>
    <item>
      <title>Re: In a Notebook, download shapefile from FTP and update existing feature service</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1330387#M9027</link>
      <description>&lt;P&gt;I submitted a case with Esri and got this back --&amp;nbsp;&lt;BR /&gt;&lt;SPAN&gt;------------------------------------------------------------------------------------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Case #&lt;/SPAN&gt;&lt;STRONG&gt;03441759&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;The behavior you've described, where FTP connections appear to be blocked when running a script in ArcGIS Online Notebooks, is likely a result of the security and network access restrictions enforced within the ArcGIS Online environment. ArcGIS Online is hosted in a secure cloud-based platform, and these measures are in place to ensure the security of the platform and its users.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If you're encountering a "ConnectionRefusedError: [Errno 111] Connection refused" error, this is indicative of the FTP connection not being allowed. To work around this limitation, I recommend using an alternative approach: consider utilizing another server where the ArcGIS API for Python can be employed to upload data to ArcGIS Online. Unfortunately, AGOL Notebooks cannot use FTP to download data due to these security restrictions.&lt;BR /&gt;------------------------------------------------------------------------------------------------------------&lt;BR /&gt;&lt;BR /&gt;So yeah -- FTP connections are blocked with AGOL and AGOL Notebooks. So don't bother going down this path.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 20:22:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1330387#M9027</guid>
      <dc:creator>RichardButgereit</dc:creator>
      <dc:date>2023-09-19T20:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: In a Notebook, download shapefile from FTP and update existing feature service</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1357513#M9381</link>
      <description>&lt;P&gt;Just to add to this:&amp;nbsp; I tried to connect to a public FTP server in an ArcGIS Online Notebook using the python module ftplib and received the&amp;nbsp;&lt;SPAN&gt;"ConnectionRefusedError: [Errno 111] Connection refused" error&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 18:02:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/in-a-notebook-download-shapefile-from-ftp-and/m-p/1357513#M9381</guid>
      <dc:creator>dzahsh</dc:creator>
      <dc:date>2023-12-06T18:02:10Z</dc:date>
    </item>
  </channel>
</rss>

